Re: [PHP] [PHP5] Super constructor?

2004-06-14 Thread Justin Patrin
Hmm, looks like my reply got lost. Here's my ideas:
Instead of parent::__construct() use Class1::__construc(). This way, you 
go straight to the top-level constructor without going through the 
middle constructors. This means that you *must* overload __construct in 
all child classes or else the constructor of the immediate parent will 
be called.

If you want to leave the option of no child constructor open, you could 
use something like:

parent::__construct();
if(strtolower(get_class($this)) == 'class2') {
  //do constructor things for class2
}
in the constructor for Class2. Then if Class3 has no constructor, the 
Class2 constructor code won't be run.

Frzzman wrote:
Yeah you understood me, thanks god :D but not fully :(
But the problem is right in your code (sorry :D)
What if I create an instance of ChildClass, its constructor won't be 
called (since it commented out), but if I un-comment its constructor, it 
will be called even if I create an instance of GrandChildClass?

Let me make some simple diagram ;)
Class1 -- Class2 -- Class3 -- Class4
If I create an instance of Class4, then Class4's constructor and 
Class1's constructor must be called, neither Class3 nor Class2.

You can see that Class1's constructor will always be called, and the 
constructor of the lowest class in the class tree will be called.

I think this is a bit complex, I can define a final function in Class1, 
and call it in every deriver class constructor, it will solve the 
problem (I think) but it's not convenience, I want it done automatically...

Any idea are welcome :D
Chris wrote:
FrzzMan wrote:
Hi guys, hey don't laugh at the subject, in fact I don't what to call 
it, so let's call it super constructor :D

Let's see following code...
class Base
{
__construct()
{
// Do something
}
}
class One extends Base
{
__construct()
{
// Do nothing
}
}
class OneMore extends One
{
__construct()
{
// Do things :D
}
}
So when I create an instance of OneMore class by:
$OneMoreInstance = new OneMore()
The __construct() method of class OneMore will be called, not the one 
of One and Base, right?

So is there any way around to make the Base class have a contructor 
that will be called everytime one of its child initialize?

My problem, I want all of the classes in my object are extend from 
one base class, so they are all have some common properties and 
function, but PHP won't implicit call the contructor (that's the 
right way)... btw, in my case, this is bad, as bad as every 
constructor would be called...

Well, hope you understand what I'm trying to say...
I'm not positive I understand you correctly, but I think this will 
answer your question.

Example Classes:
class ParentClass
{
   function __construct()
   {
   echo 'ParentClass::__construct()',\r\n;
   }
}
class ChildClass extends ParentClass
{
/*
   function __construct()
   {
   parent::__construct();
   echo 'ChildClass::__construct()',\r\n;
   }
//*/
}
class GrandChildClass extends ChildClass
{
   function __construct()
   {
   parent::__construct();
   echo 'GrandChildClass::__construct()',\r\n;
   }
}
__construct is just like any other method, if you overload it 
(redefine it in a child class), the parent method will not be called 
automatically, so, if you want the functionality of both, you can call 
the parent constructor from the childs constructor. If the direct 
parent doesn't have a constructor, the next parent's constructor is 
checked and so on.

In the above example when defining a GrandChildClass, its constructor 
and the ParentClass::__construct() are will both run. If you uncomment 
ChildClass::__construct(), it will run as well.

--
paperCrane Justin Patrin
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[PHP] [PHP5] Super constructor?

2004-06-13 Thread FrzzMan
Hi guys, hey don't laugh at the subject, in fact I don't what to call 
it, so let's call it super constructor :D

Let's see following code...
class Base
{
__construct()
{
// Do something
}
}
class One extends Base
{
__construct()
{
// Do nothing
}
}
class OneMore extends One
{
__construct()
{
// Do things :D
}
}
So when I create an instance of OneMore class by:
$OneMoreInstance = new OneMore()
The __construct() method of class OneMore will be called, not the one of 
One and Base, right?

So is there any way around to make the Base class have a contructor that 
will be called everytime one of its child initialize?

My problem, I want all of the classes in my object are extend from one 
base class, so they are all have some common properties and function, 
but PHP won't implicit call the contructor (that's the right way)... 
btw, in my case, this is bad, as bad as every constructor would be called...

Well, hope you understand what I'm trying to say...
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Re: [PHP] [PHP5] Super constructor?

2004-06-13 Thread Chris
FrzzMan wrote:
Hi guys, hey don't laugh at the subject, in fact I don't what to call 
it, so let's call it super constructor :D

Let's see following code...
class Base
{
__construct()
{
// Do something
}
}
class One extends Base
{
__construct()
{
// Do nothing
}
}
class OneMore extends One
{
__construct()
{
// Do things :D
}
}
So when I create an instance of OneMore class by:
$OneMoreInstance = new OneMore()
The __construct() method of class OneMore will be called, not the one 
of One and Base, right?

So is there any way around to make the Base class have a contructor 
that will be called everytime one of its child initialize?

My problem, I want all of the classes in my object are extend from one 
base class, so they are all have some common properties and function, 
but PHP won't implicit call the contructor (that's the right way)... 
btw, in my case, this is bad, as bad as every constructor would be 
called...

Well, hope you understand what I'm trying to say...
I'm not positive I understand you correctly, but I think this will 
answer your question.

Example Classes:
class ParentClass
{
   function __construct()
   {
   echo 'ParentClass::__construct()',\r\n;
   }
}
class ChildClass extends ParentClass
{
/*
   function __construct()
   {
   parent::__construct();
   echo 'ChildClass::__construct()',\r\n;
   }
//*/
}
class GrandChildClass extends ChildClass
{
   function __construct()
   {
   parent::__construct();
   echo 'GrandChildClass::__construct()',\r\n;
   }
}
__construct is just like any other method, if you overload it (redefine 
it in a child class), the parent method will not be called 
automatically, so, if you want the functionality of both, you can call 
the parent constructor from the childs constructor. If the direct parent 
doesn't have a constructor, the next parent's constructor is checked and 
so on.

In the above example when defining a GrandChildClass, its constructor 
and the ParentClass::__construct() are will both run. If you uncomment 
ChildClass::__construct(), it will run as well.

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Re: [PHP] [PHP5] Super constructor?

2004-06-13 Thread FrzzMan
Yeah you understood me, thanks god :D but not fully :(
But the problem is right in your code (sorry :D)
What if I create an instance of ChildClass, its constructor won't be 
called (since it commented out), but if I un-comment its constructor, it 
will be called even if I create an instance of GrandChildClass?

Let me make some simple diagram ;)
Class1 -- Class2 -- Class3 -- Class4
If I create an instance of Class4, then Class4's constructor and 
Class1's constructor must be called, neither Class3 nor Class2.

You can see that Class1's constructor will always be called, and the 
constructor of the lowest class in the class tree will be called.

I think this is a bit complex, I can define a final function in Class1, 
and call it in every deriver class constructor, it will solve the 
problem (I think) but it's not convenience, I want it done automatically...

Any idea are welcome :D
Chris wrote:
FrzzMan wrote:
Hi guys, hey don't laugh at the subject, in fact I don't what to call 
it, so let's call it super constructor :D

Let's see following code...
class Base
{
__construct()
{
// Do something
}
}
class One extends Base
{
__construct()
{
// Do nothing
}
}
class OneMore extends One
{
__construct()
{
// Do things :D
}
}
So when I create an instance of OneMore class by:
$OneMoreInstance = new OneMore()
The __construct() method of class OneMore will be called, not the one 
of One and Base, right?

So is there any way around to make the Base class have a contructor 
that will be called everytime one of its child initialize?

My problem, I want all of the classes in my object are extend from one 
base class, so they are all have some common properties and function, 
but PHP won't implicit call the contructor (that's the right way)... 
btw, in my case, this is bad, as bad as every constructor would be 
called...

Well, hope you understand what I'm trying to say...
I'm not positive I understand you correctly, but I think this will 
answer your question.

Example Classes:
class ParentClass
{
   function __construct()
   {
   echo 'ParentClass::__construct()',\r\n;
   }
}
class ChildClass extends ParentClass
{
/*
   function __construct()
   {
   parent::__construct();
   echo 'ChildClass::__construct()',\r\n;
   }
//*/
}
class GrandChildClass extends ChildClass
{
   function __construct()
   {
   parent::__construct();
   echo 'GrandChildClass::__construct()',\r\n;
   }
}
__construct is just like any other method, if you overload it (redefine 
it in a child class), the parent method will not be called 
automatically, so, if you want the functionality of both, you can call 
the parent constructor from the childs constructor. If the direct parent 
doesn't have a constructor, the next parent's constructor is checked and 
so on.

In the above example when defining a GrandChildClass, its constructor 
and the ParentClass::__construct() are will both run. If you uncomment 
ChildClass::__construct(), it will run as well.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Re: [PHP] [PHP5] Super constructor?

2004-06-13 Thread Chris
Well, when you instantiate Class4 PHP will check for it's constructor. 
If it has one, it will call it. If not it will check Class3, then 
Class2, then Class1.

Once it finds a constructor, it stops looking for more, but if you want 
it to resume checking the the parents for constructors you must call 
parent::_construct() from  the one it found.

If you define a constructor in Class4, It will run in all but one 
circumstance. If a child has a constructor, but does not call 
parent::_construct() from it. That would stop this waterfall effect.

FrzzMan wrote:
Yeah you understood me, thanks god :D but not fully :(
But the problem is right in your code (sorry :D)
What if I create an instance of ChildClass, its constructor won't be 
called (since it commented out), but if I un-comment its constructor, 
it will be called even if I create an instance of GrandChildClass?

Let me make some simple diagram ;)
Class1 -- Class2 -- Class3 -- Class4
If I create an instance of Class4, then Class4's constructor and 
Class1's constructor must be called, neither Class3 nor Class2.

You can see that Class1's constructor will always be called, and the 
constructor of the lowest class in the class tree will be called.

I think this is a bit complex, I can define a final function in 
Class1, and call it in every deriver class constructor, it will solve 
the problem (I think) but it's not convenience, I want it done 
automatically...

Any idea are welcome :D
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Re: [PHP] [PHP5] Super constructor?

2004-06-13 Thread Chris
Because /$GrandChildClass;/ Is an object. So when you go to echo it, PHP 
tries to convert it to a string.

Pham Cong Dinh wrote:
Hi all,
I tested Chris's code:
?php
class *ParentClass*
{
   function __construct()
   {
   echo 'ParentClass::__construct()',\r\n;
   }
}
class *ChildClass *extends ParentClass
{
/*
   function __construct()
   {
   parent::__construct();
   echo 'ChildClass::__construct()',\r\n;
   }
//*/
}
class *GrandChildClass *extends ChildClass
{
   function __construct()
   {
   parent::__construct();
   echo 'GrandChildClass::__construct()',\r\n;
   }
}
$GrandChildClass = new GrandChildClass();
echo $GrandChildClass;
?
It resulted:
ParentClass::__construct()
GrandChildClass::__construct()
Object id #1
Could anyone kindly tell me why the string Object id #1 is printed?
Thanks
Dinh
Chris wrote:
FrzzMan wrote:
Hi guys, hey don't laugh at the subject, in fact I don't what to 
call it, so let's call it super constructor :D

Let's see following code...
class Base
{
__construct()
{
// Do something
}
}
class One extends Base
{
__construct()
{
// Do nothing
}
}
class OneMore extends One
{
__construct()
{
// Do things :D
}
}
So when I create an instance of OneMore class by:
$OneMoreInstance = new OneMore()
The __construct() method of class OneMore will be called, not the 
one of One and Base, right?

So is there any way around to make the Base class have a contructor 
that will be called everytime one of its child initialize?

My problem, I want all of the classes in my object are extend from 
one base class, so they are all have some common properties and 
function, but PHP won't implicit call the contructor (that's the 
right way)... btw, in my case, this is bad, as bad as every 
constructor would be called...

Well, hope you understand what I'm trying to say...
I'm not positive I understand you correctly, but I think this will 
answer your question.

Example Classes:
class ParentClass
{
   function __construct()
   {
   echo 'ParentClass::__construct()',\r\n;
   }
}
class ChildClass extends ParentClass
{
/*
   function __construct()
   {
   parent::__construct();
   echo 'ChildClass::__construct()',\r\n;
   }
//*/
}
class GrandChildClass extends ChildClass
{
   function __construct()
   {
   parent::__construct();
   echo 'GrandChildClass::__construct()',\r\n;
   }
}
__construct is just like any other method, if you overload it 
(redefine it in a child class), the parent method will not be called 
automatically, so, if you want the functionality of both, you can 
call the parent constructor from the childs constructor. If the 
direct parent doesn't have a constructor, the next parent's 
constructor is checked and so on.

In the above example when defining a GrandChildClass, its constructor 
and the ParentClass::__construct() are will both run. If you 
uncomment ChildClass::__construct(), it will run as well.


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Re: [PHP] [PHP5] Super constructor?

2004-06-13 Thread FrzzMan
Try var_dump($GrandChildClass) or print_r($GrandChildClass) everytime 
you need to inspect variable content. Not echo...

PS: Hi there Vietnamese guy, same here ;)
Chris wrote:
Because /$GrandChildClass;/ Is an object. So when you go to echo it, PHP 
tries to convert it to a string.

Pham Cong Dinh wrote:
Hi all,
I tested Chris's code:
?php
class *ParentClass*
{
   function __construct()
   {
   echo 'ParentClass::__construct()',\r\n;
   }
}
class *ChildClass *extends ParentClass
{
/*
   function __construct()
   {
   parent::__construct();
   echo 'ChildClass::__construct()',\r\n;
   }
//*/
}
class *GrandChildClass *extends ChildClass
{
   function __construct()
   {
   parent::__construct();
   echo 'GrandChildClass::__construct()',\r\n;
   }
}
$GrandChildClass = new GrandChildClass();
echo $GrandChildClass;
?
It resulted:
ParentClass::__construct()
GrandChildClass::__construct()
Object id #1
Could anyone kindly tell me why the string Object id #1 is printed?
Thanks
Dinh
Chris wrote:
FrzzMan wrote:
Hi guys, hey don't laugh at the subject, in fact I don't what to 
call it, so let's call it super constructor :D

Let's see following code...
class Base
{
__construct()
{
// Do something
}
}
class One extends Base
{
__construct()
{
// Do nothing
}
}
class OneMore extends One
{
__construct()
{
// Do things :D
}
}
So when I create an instance of OneMore class by:
$OneMoreInstance = new OneMore()
The __construct() method of class OneMore will be called, not the 
one of One and Base, right?

So is there any way around to make the Base class have a contructor 
that will be called everytime one of its child initialize?

My problem, I want all of the classes in my object are extend from 
one base class, so they are all have some common properties and 
function, but PHP won't implicit call the contructor (that's the 
right way)... btw, in my case, this is bad, as bad as every 
constructor would be called...

Well, hope you understand what I'm trying to say...
I'm not positive I understand you correctly, but I think this will 
answer your question.

Example Classes:
class ParentClass
{
   function __construct()
   {
   echo 'ParentClass::__construct()',\r\n;
   }
}
class ChildClass extends ParentClass
{
/*
   function __construct()
   {
   parent::__construct();
   echo 'ChildClass::__construct()',\r\n;
   }
//*/
}
class GrandChildClass extends ChildClass
{
   function __construct()
   {
   parent::__construct();
   echo 'GrandChildClass::__construct()',\r\n;
   }
}
__construct is just like any other method, if you overload it 
(redefine it in a child class), the parent method will not be called 
automatically, so, if you want the functionality of both, you can 
call the parent constructor from the childs constructor. If the 
direct parent doesn't have a constructor, the next parent's 
constructor is checked and so on.

In the above example when defining a GrandChildClass, its constructor 
and the ParentClass::__construct() are will both run. If you 
uncomment ChildClass::__construct(), it will run as well.


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php