IT Community - Software Programming, Web Development and Technical Support

PHP Classes For Beginners

This is a discussion on PHP Classes For Beginners within the PHP Programming forums, part of the Web Development category; eacy to use constructors in php class. when we creating a objects from using the new keyword the constructor function ...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Web Development > PHP Programming

Register FAQ Members List Calendar Mark Forums Read
  #21 (permalink)  
Old 04-23-2008, 06:25 AM
jegan jegan is offline
D-Web Sr.Programmer
 
Join Date: Jul 2007
Posts: 161
jegan is on a distinguished road
Default Re: PHP Classes For Beginners

eacy to use constructors in php class. when we creating a objects from using the new keyword the constructor function automatically executes
PHP Code:
class Box
{
    var  
width;
    var 
height;

        function 
box($pmWidth,$pmHeight)
{
    
$this->width    =$pmWidth;
   
$this->height=,$pmHeight;
}
function 
area()
{
    return 
$this->width*$this->height;
}
}

//This is syntax for Calling of constructor function 
$object        =    new Box(100,100);    //The new key word used to create an object
echo “Area of object box:$object->area(); 
output

Area of object box :1000
__________________
Thanks & Regards,
Jegan CBK
"We will either find a way, or make one!”
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #22 (permalink)  
Old 04-23-2008, 06:27 AM
jegan jegan is offline
D-Web Sr.Programmer
 
Join Date: Jul 2007
Posts: 161
jegan is on a distinguished road
Default Re: PHP Classes For Beginners

Using destructors
PHP Code:
class Box
{
    var  
width;
    var 
height;
    
        function ~
box()
        {
           
//Code for release the memory  
        
}
}

Box1=new box ();    //it’s call the constructor function 
Delete box1;           //it’s call destructor function 
__________________
Thanks & Regards,
Jegan CBK
"We will either find a way, or make one!”
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #23 (permalink)  
Old 04-24-2008, 06:14 AM
saravanan saravanan is offline
D-Web Sr.Programmer
 
Join Date: Jul 2007
Posts: 181
saravanan is on a distinguished road
Default Re: PHP Classes For Beginners

what is inheritance?
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #24 (permalink)  
Old 04-24-2008, 06:17 AM
jegan jegan is offline
D-Web Sr.Programmer
 
Join Date: Jul 2007
Posts: 161
jegan is on a distinguished road
Default Re: PHP Classes For Beginners

What is inheritance?

Probably the greatest feature of the PHP OOP model is Inheritance. Inheritance is the ability of PHP to extend classes (child classes) that inherit the characteristics of the parent class. The resulting object of an extend class has all the characteristics of the parent class, plus whatever is in the new child, or extended class. In this instance we will extend the vehicle class and add a motorcycle. A motorcycle is still a vehicle and shares many of the same attributes such as price, drive etc. But a motorcycle has some unique features that a car does not.
__________________
Thanks & Regards,
Jegan CBK
"We will either find a way, or make one!”
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #25 (permalink)  
Old 04-24-2008, 06:31 AM
saravanan saravanan is offline
D-Web Sr.Programmer
 
Join Date: Jul 2007
Posts: 181
saravanan is on a distinguished road
Default Re: PHP Classes For Beginners

give me simple example for inheritance?
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #26 (permalink)  
Old 04-24-2008, 06:32 AM
jegan jegan is offline
D-Web Sr.Programmer
 
Join Date: Jul 2007
Posts: 161
jegan is on a distinguished road
Default Re: PHP Classes For Beginners

Ans
I have written a program that inherits our Box class. Here the base class is Box and the derived class is BoxChild.
We have a scenario to find the weight of a box. So we need to write a new class with width, height and weigh member variables. But this will lead to us to do lot of workload. But we can inherit our Box object and we can add required thinks in child class.
PHP Code:
//Base class
<?php
class Box
{
    var  
width;
    var 
height;

        function 
box($pmWidth,$pmHeight)
{
    
$this->width    =$pmWidth;
   
$this->height=,$pmHeight;
}
function 
area()
{
    return 
$this->width*$this->height;
}
}
//Child class
Class BoxChild extends Box
{
    var 
$weight
    
function BoxChild($pmHeight,$pmWidth,$pmWeight)
    {
        
$this->weigth=$pmWeight;
         
parent::height=$pmHeight;
  
parent::width=$pmWidth
}
function 
getWeight()
{
    return 
$this->weight;
}
}
?>
__________________
Thanks & Regards,
Jegan CBK
"We will either find a way, or make one!”
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #27 (permalink)  
Old 04-25-2008, 06:21 AM
saravanan saravanan is offline
D-Web Sr.Programmer
 
Join Date: Jul 2007
Posts: 181
saravanan is on a distinguished road
Default Re: PHP Classes For Beginners

what is access modifiers in php?
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #28 (permalink)  
Old 04-25-2008, 06:24 AM
jegan jegan is offline
D-Web Sr.Programmer
 
Join Date: Jul 2007
Posts: 161
jegan is on a distinguished road
Default Re: PHP Classes For Beginners

1. Restricting access to class properties (variable).
2. Restricting access to methods.
3. They support the concept of encapsulation, which promotes the idea of hiding functionality.
4. Access modifiers allow you to define who does or doesn’t have access to certain features in class
__________________
Thanks & Regards,
Jegan CBK
"We will either find a way, or make one!”
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #29 (permalink)  
Old 04-25-2008, 06:41 AM
saravanan saravanan is offline
D-Web Sr.Programmer
 
Join Date: Jul 2007
Posts: 181
saravanan is on a distinguished road
Default Re: PHP Classes For Beginners

what are the access modifiers available in php?
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #30 (permalink)  
Old 04-25-2008, 06:43 AM
jegan jegan is offline
D-Web Sr.Programmer
 
Join Date: Jul 2007
Posts: 161
jegan is on a distinguished road
Default Re: PHP Classes For Beginners

Access Modifiers
1. public
2. private
3. protected
4. Final
__________________
Thanks & Regards,
Jegan CBK
"We will either find a way, or make one!”

Last edited by jegan : 04-28-2008 at 06:00 AM.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #31 (permalink)  
Old 04-28-2008, 06:01 AM
saravanan saravanan is offline
D-Web Sr.Programmer
 
Join Date: Jul 2007
Posts: 181
saravanan is on a distinguished road
Default Re: PHP Classes For Beginners

how to use public access modifier in php?
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #32 (permalink)  
Old 04-28-2008, 06:03 AM
jegan jegan is offline
D-Web Sr.Programmer
 
Join Date: Jul 2007
Posts: 161
jegan is on a distinguished road
Lightbulb Re: PHP Classes For Beginners

Public variables and functions are accessible from anywhere in your script.

We can see in the below example that the public variable $num is set from user space and we call a public method that adds two the number and returns the value of $num+2. Having our properties (variables) visible or accessible from any part of our script works in our favors here, but it can also work against us. A could arise if we lost track of our values and changed the value of $num. To counter this problem we can create a method to set the value for us. Even with this in place it is still possible for somebody to simply access the $num variable. So we make the variable private. This ensures us that the property is only available within the class itself. It is private to the calling class.
PHP Code:

<?php

class mathematics
{
    
/*** a number ***/
    
public $num;
    public function 
addTwo()
       {
         Return 
$this->num+2;
    }
}
$math = new mathematics;

$math->num 2;

echo 
$math->addTwo();

?>
__________________
Thanks & Regards,
Jegan CBK
"We will either find a way, or make one!”
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #33 (permalink)  
Old 04-28-2008, 06:05 AM
saravanan saravanan is offline
D-Web Sr.Programmer
 
Join Date: Jul 2007
Posts: 181
saravanan is on a distinguished road
Default Re: PHP Classes For Beginners

how to use protected access modifier in php?
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #34 (permalink)  
Old 04-28-2008, 06:07 AM
jegan jegan is offline
D-Web Sr.Programmer
 
Join Date: Jul 2007
Posts: 161
jegan is on a distinguished road
Default Re: PHP Classes For Beginners

Using Protected
Variables and functions marked as protected are accessible only through the object that owns them, whether or not they are declared in that object's class or whether they have descended from a parent class. Consider the following code:
PHP Code:
<?php
    
class dog {
        public 
$Name;
        private function 
getName() {
            return 
$this->Name;
        }
    }

    class 
poodle extends dog {
        public function 
bark() {
            print 
"'Woof', says " $this->getName();
        }
    }
    
    
$poppy = new poodle;
    
$poppy->Name "Poppy";
    
$poppy->bark();
?>
In that code, the class poodle extends from class dog, class dog has a public variable $Name and a private function getName(), and class poodle has a public function called bark(). So, we create a poodle, give it a $Name value of "Poppy" (the $Name variable comes from the dog class), then ask it to bark(). The bark() function is public, which means we can call it as shown above, so this is all well and good.
However, note that the bark() function calls the getName() function, which is part of the dog class and was marked private - this will stop the script from working, because private variables and functions cannot be accessed from inherited classes. That is, we cannot access private dog functions and variables from inside the poodle class.
Now try changing bark() to protected, and all should become clear - the variable is still not available to the world as a whole, but handles inheritance as you would expect, which means that we can access getName() from inside poodle.
__________________
Thanks & Regards,
Jegan CBK
"We will either find a way, or make one!”
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #35 (permalink)  
Old 04-28-2008, 06:08 AM
saravanan saravanan is offline
D-Web Sr.Programmer
 
Join Date: Jul 2007
Posts: 181
saravanan is on a distinguished road
Default Re: PHP Classes For Beginners

how to use private access modifiers in php?
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #36 (permalink)  
Old 04-28-2008, 06:09 AM
jegan jegan is offline
D-Web Sr.Programmer
 
Join Date: Jul 2007
Posts: 161
jegan is on a distinguished road
Default Re: PHP Classes For Beginners

Private Variables
Private variable and method accessible only within the class. Not possible from outside of class.

PHP Code:
<?php
    
class dog 
{
        Private 
$Name;
        }

    class 
poodle extends dog
 
{ }

    
$poppy = new poodle;
    
$poppy->Name "Poppy";
    print (
$poppy);

?>
The derived class object try to access the private member variable of class dog. but it not possible to access from out side of class.
__________________
Thanks & Regards,
Jegan CBK
"We will either find a way, or make one!”
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #37 (permalink)  
Old 04-29-2008, 06:58 AM
saravanan saravanan is offline
D-Web Sr.Programmer
 
Join Date: Jul 2007
Posts: 181
saravanan is on a distinguished road
Default Re: PHP Classes For Beginners

what is inheritance ?
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #38 (permalink)  
Old 04-29-2008, 07:01 AM
jegan jegan is offline
D-Web Sr.Programmer
 
Join Date: Jul 2007
Posts: 161
jegan is on a distinguished road
Default Re: PHP Classes For Beginners

inheritance


Probably the greatest feature of the PHP OOP model is Inheritance. Inheritance is the ability of PHP to extend classes (child classes) that inherit the characteristics of the parent class. The resulting object of an extend class has all the characteristics of the parent class, plus whatever is in the new child, or extended class.

The most common is simplifying and reducing instances of redundant code that is code reusability.
__________________
Thanks & Regards,
Jegan CBK
"We will either find a way, or make one!”
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #39 (permalink)  
Old 04-29-2008, 07:01 AM
jegan jegan is offline
D-Web Sr.Programmer
 
Join Date: Jul 2007
Posts: 161
jegan is on a distinguished road
Default Re: PHP Classes For Beginners

what are types of inheritance in php?
__________________
Thanks & Regards,
Jegan CBK
"We will either find a way, or make one!”
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #40 (permalink)  
Old 04-29-2008, 07:03 AM
jegan jegan is offline
D-Web Sr.Programmer
 
Join Date: Jul 2007
Posts: 161
jegan is on a distinguished road
Default Re: PHP Classes For Beginners

Type of Inheritance

1.Single inheritance

2.Multi-level inheritance

3.Multiple inheritance can’t achieve in php.
__________________
Thanks & Regards,
Jegan CBK
"We will either find a way, or make one!”
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On