Welcome Guest, Not a member yet? Register   Sign In
New to programming and trying to understand it all.
#9

[eluser]Brandon Dickson[/eluser]
You never actually call the constructor, it gets called by php when a new instance of a class is created.

Let me see if this helps, i've been known to ramble..

Code:
class Parent_class
{
         function Parent_class(){
                /* this is only called when a new instance is created e.g. new Parent_class()*/
                $this->message = "I'm the parent class!";
         }
}

class First_child extends Parent_class
{
         function First_child(){
               $this->message = "I'm the first child!";
         }
}

class Second_child extends Parent_class
{
/* no constructor here... */
}

class Third_child extends Parent_class
{
        function Third_child(){
                parent::Parent_class() /* calling the constructor of the parent class */
                $this->new_message = "I'm the third child!";
        }
}

$parent = new Parent_class();

/* outputs : I'm the parent class! */
echo $parent->message;

$first = new First_child();

/* outputs : I'm the first child! ( the constructor was overloaded ) */
echo $first->message;

$second = new Second_child();

/* output :I'm the parent class!
( there is no constructor for the second child class,
so the parent constructor is called )*/
echo $second->message;

$third = new Third_child();

/* output : I'm the parent class! ( because we called the parent constructor inside the child constructor )*/
echo $third->message;

/* output : I'm the third child! */
echo $third->new_message;

Maybe this helps.

-Brandon
[code]


Messages In This Thread
New to programming and trying to understand it all. - by El Forum - 05-31-2008, 11:22 PM
New to programming and trying to understand it all. - by El Forum - 06-01-2008, 12:01 AM
New to programming and trying to understand it all. - by El Forum - 06-01-2008, 12:37 AM
New to programming and trying to understand it all. - by El Forum - 06-01-2008, 12:54 AM
New to programming and trying to understand it all. - by El Forum - 06-01-2008, 09:09 AM
New to programming and trying to understand it all. - by El Forum - 06-03-2008, 09:18 PM
New to programming and trying to understand it all. - by El Forum - 06-03-2008, 10:19 PM
New to programming and trying to understand it all. - by El Forum - 06-03-2008, 10:47 PM
New to programming and trying to understand it all. - by El Forum - 06-03-2008, 11:20 PM
New to programming and trying to understand it all. - by El Forum - 06-04-2008, 07:50 PM



Theme © iAndrew 2016 - Forum software by © MyBB