php5 class initialization

php.internals

t

22 years ago
<?php class a { function __construct() { echo '__construct()'; } function a() { echo 'a()'; } } $a=new a; ?> Outputs ... a() ... when I would expect ... __construct() ... is this a bug, some sort of new (only seeing this occur since RC1) feature, or some option I'm supposed to switch off ?

Andi Gutmans

22 years ago
We use the last defined constructor. We fixed a problem in B4 where old style constructors didn't always work. Andi At 12:30 AM 3/22/2004 +0000, t wrote:

Marcus Börger

22 years ago
Hello Andi, Monday, March 22, 2004, 8:00:42 AM, you wrote:
> We use the last defined constructor. We fixed a problem in B4 where old > style constructors didn't always work.
If so that's a bug. That's absolutley not understandable.
> Andi
> At 12:30 AM 3/22/2004 +0000, t wrote: >><?php >>class a >>{ >>function __construct() >>{ >>echo '__construct()'; >>} >> >>function a() >>{ >>echo 'a()'; >>} >>} >> >>$a=new a; >>?> >> >>Outputs ... a() ... when I would expect ... __construct() ... is this a bug, >>some sort of new (only seeing this occur since RC1) feature, or some option >>I'm supposed to switch off ? >> >>-- >>PHP Internals - PHP Runtime Development Mailing List >>To unsubscribe, visit: http://www.php.net/unsub.php
-- Best regards, Marcus mailto:helly@php.net

Andi Gutmans

22 years ago
What would you expect? That if a new style constructor is defined we always use that and allow old-style as regular method? At 08:56 AM 3/22/2004 +0100, Marcus Boerger wrote:

Sebastian Bergmann

22 years ago
Andi Gutmans wrote:
> What would you expect? That if a new style constructor is defined we > always use that and allow old-style as regular method?
- Old-Style Constructor Only: Use it, but issue E_STRICT. - Old-Style and New-Style Constructor: Use the New-Style Constructor and issue an E_STRICT.
-- Sebastian Bergmann http://sebastian-bergmann.de/ http://phpOpenTracker.de/ Das Buch zu PHP 5: http://professionelle-softwareentwicklung-mit-php5.de/

Andi Gutmans

22 years ago
At 11:36 AM 3/22/2004 +0100, Sebastian Bergmann wrote:
>Andi Gutmans wrote: > > What would you expect? That if a new style constructor is defined we > > always use that and allow old-style as regular method? > > - Old-Style Constructor Only: Use it, but issue E_STRICT. > > - Old-Style and New-Style Constructor: Use the New-Style Constructor and > issue an E_STRICT.
I'm not sure if E_STRICT isn't a bit too aggressive. Most people will want to use E_STRICT IMO but have lots and lots of classes such as PEAR classes which use old-style constructors. I don't want them to turn off E_STRICT because of this. Andi

Sebastian Bergmann

22 years ago
Andi Gutmans wrote:
> Most people will want to use E_STRICT IMO but have lots and lots of > classes such as PEAR classes which use old-style constructors.
Point taken. But the above does not hold for both styles in the same class for which I highly recommend the E_STRICT.
-- Sebastian Bergmann http://sebastian-bergmann.de/ http://phpOpenTracker.de/ Das Buch zu PHP 5: http://professionelle-softwareentwicklung-mit-php5.de/

Andi Gutmans

22 years ago
At 12:12 PM 3/22/2004 +0100, Sebastian Bergmann wrote:
>Andi Gutmans wrote: > > Most people will want to use E_STRICT IMO but have lots and lots of > > classes such as PEAR classes which use old-style constructors. > > Point taken. But the above does not hold for both styles in the same > class for which I highly recommend the E_STRICT.
We already have an E_STRICT for that. Andi

Sebastian Bergmann

22 years ago
Andi Gutmans wrote:
> We already have an E_STRICT for that.
Good :)
-- Sebastian Bergmann http://sebastian-bergmann.de/ http://phpOpenTracker.de/ Das Buch zu PHP 5: http://professionelle-softwareentwicklung-mit-php5.de/

Andrei Zmievski

22 years ago
On Mon, 22 Mar 2004, Andi Gutmans wrote:
> What would you expect? That if a new style constructor is defined we always > use that and allow old-style as regular method?
Yep. - Andrei

George Schlossnagle

22 years ago
On Mar 23, 2004, at 11:53 AM, Andrei Zmievski wrote:
> On Mon, 22 Mar 2004, Andi Gutmans wrote: >> What would you expect? That if a new style constructor is defined we >> always >> use that and allow old-style as regular method? > > Yep.
I agree with the part about the new-style constructor always being preferred to the old-style constructor, but besides being really confusing, where do you being able to call the old-style as a regular method coming in handy? George

Andrei Zmievski

22 years ago
On Tue, 23 Mar 2004, George Schlossnagle wrote:
> I agree with the part about the new-style constructor always being > preferred to the old-style constructor, but besides being really > confusing, where do you being able to call the old-style as a regular > method coming in handy?
I don't really. I just think if they both exist then the old style constructor should be a regular method. - Andrei

Rasmus Lerdorf

22 years ago
On Tue, 23 Mar 2004, George Schlossnagle wrote:
> On Mar 23, 2004, at 11:53 AM, Andrei Zmievski wrote: > > > On Mon, 22 Mar 2004, Andi Gutmans wrote: > >> What would you expect? That if a new style constructor is defined we > >> always > >> use that and allow old-style as regular method? > > > > Yep. > > > I agree with the part about the new-style constructor always being > preferred to the old-style constructor, but besides being really > confusing, where do you being able to call the old-style as a regular > method coming in handy?
I could see people doing: class foo { function foo() { ...constructor stuff... } function __construct() { $this->foo(); } } In order to make the class work on both PHP4 and PHP5 in which case it would be essential for foo() to be callable as a regular method in the class. -Rasmus

Christian Schneider

22 years ago
Rasmus Lerdorf wrote:
> I could see people doing: > > class foo { > function foo() { > ...constructor stuff... > } > function __construct() { > $this->foo(); > } > }
Why not just leave out __construct in this case? Or am I missing something? This looks pretty ugly to me. And if you really, really want it you can still have a method init() which is called by foo() and __construct(). - Chris

Brad Fisher

22 years ago
Rasmus Lerdorf wrote:
> I could see people doing: > > class foo { > function foo() { > ...constructor stuff... > } > function __construct() { > $this->foo(); > } > }
I actually prefer it the other way around... foo would call __construct in PHP4, and __construct would be called directly in PHP5 (with foo being ignored). As I understand the docs on php.net regading PHP5, the __construct method has precedence over foo as a constructor. I've also run into a case where a class worked fine in PHP4 and not in PHP5 because of a method existing with the same name as the class. It was seen as a constructor, and not a method, even though it is always called statically. For example: class Log { function log($msg) { // Log the message here } } Always called as: Log::log("log this message"); No instance of Log is ever created, which seemed to work in PHP4, but throws errors in PHP5 about not being able to call Log::log statically (I assume because it thinks its a constructor). This probably isn't good practice to use this sort of naming, but I didn't even think of Log::log as a constructor in this case since the object itself is never instanciated. Of course with the static method/class qualifiers in PHP5, I could explicity way that in the syntax, but such things aren't BC with PHP4...
> In order to make the class work on both PHP4 and PHP5 in which case it > would be essential for foo() to be callable as a regular method in the > class.
Yes, I like the unified constructor concept, and doing it this way makes that work in PHP4 as well (at least as long as you have a base class which implements both forms of constructors). -Brad

Rasmus Lerdorf

22 years ago
On Tue, 23 Mar 2004, Brad Fisher wrote:
> Rasmus Lerdorf wrote: > > > I could see people doing: > > > > class foo { > > function foo() { > > ...constructor stuff... > > } > > function __construct() { > > $this->foo(); > > } > > } > > I actually prefer it the other way around... foo would call __construct in > PHP4, and __construct would be called directly in PHP5 (with foo being > ignored). As I understand the docs on php.net regading PHP5, the __construct > method has precedence over foo as a constructor. I've also run into a case > where a class worked fine in PHP4 and not in PHP5 because of a method > existing with the same name as the class. It was seen as a constructor, and > not a method, even though it is always called statically.
Sure, but George was asking for a case. I think the case where you have some existing PHP4 code that you want to make minimal changes to but you might have a little bit of PHP5-specific code to run in the constructor I could see this sort of chaining being something people want. I am not saying that is the way people should create a portable class, just that I could see such code popping up. And the question was what to do about the old-style constructor method. I think it would be very confusing if it was some sort of illegal method name that wasn't callable. -Rasmus

George Schlossnagle

22 years ago
On Mar 23, 2004, at 1:11 PM, Rasmus Lerdorf wrote:
> On Tue, 23 Mar 2004, Brad Fisher wrote: > > Sure, but George was asking for a case. I think the case where you > have > some existing PHP4 code that you want to make minimal changes to but > you > might have a little bit of PHP5-specific code to run in the > constructor I > could see this sort of chaining being something people want. I am not > saying that is the way people should create a portable class, just > that I > could see such code popping up. And the question was what to do about > the > old-style constructor method. I think it would be very confusing if it > was some sort of illegal method name that wasn't callable.
You sold me on the usefulness. George

Stanislav Malyshev

22 years ago
RL>> Sure, but George was asking for a case. I think the case where you have RL>> some existing PHP4 code that you want to make minimal changes to but you RL>> might have a little bit of PHP5-specific code to run in the constructor I RL>> could see this sort of chaining being something people want. I am not Why the said people won't just insert the PHP5-specific code right into the constructor? Why they need the second one?
-- Stanislav Malyshev, Zend Products Engineer stas@zend.com http://www.zend.com/ +972-3-6139665 ext.109

Rasmus Lerdorf

22 years ago
On Tue, 23 Mar 2004, Stanislav Malyshev wrote:
> RL>> Sure, but George was asking for a case. I think the case where you have > RL>> some existing PHP4 code that you want to make minimal changes to but you > RL>> might have a little bit of PHP5-specific code to run in the constructor I > RL>> could see this sort of chaining being something people want. I am not > > Why the said people won't just insert the PHP5-specific code right into > the constructor? Why they need the second one?
To avoid having it in 2 places. -Rasmus

Andi Gutmans

22 years ago
At 09:16 AM 3/23/2004 -0800, Rasmus Lerdorf wrote:
>On Tue, 23 Mar 2004, George Schlossnagle wrote: > > On Mar 23, 2004, at 11:53 AM, Andrei Zmievski wrote: > > > > > On Mon, 22 Mar 2004, Andi Gutmans wrote: > > >> What would you expect? That if a new style constructor is defined we > > >> always > > >> use that and allow old-style as regular method? > > > > > > Yep. > > > > > > I agree with the part about the new-style constructor always being > > preferred to the old-style constructor, but besides being really > > confusing, where do you being able to call the old-style as a regular > > method coming in handy? > >I could see people doing: > > class foo { > function foo() { > ...constructor stuff... > } > function __construct() { > $this->foo(); > } > } > >In order to make the class work on both PHP4 and PHP5 in which case it >would be essential for foo() to be callable as a regular method in the >class.
This is possible already. Andi

Andi Gutmans

22 years ago
At 09:16 AM 3/23/2004 -0800, Rasmus Lerdorf wrote:
> > I agree with the part about the new-style constructor always being > > preferred to the old-style constructor, but besides being really > > confusing, where do you being able to call the old-style as a regular > > method coming in handy? > >I could see people doing: > > class foo { > function foo() { > ...constructor stuff... > } > function __construct() { > $this->foo(); > } > } > >In order to make the class work on both PHP4 and PHP5 in which case it >would be essential for foo() to be callable as a regular method in the >class.
Didn't see the context :) Andi

Marcus Börger

22 years ago
Hello Andi, Monday, March 22, 2004, 11:24:03 AM, you wrote:
> What would you expect? That if a new style constructor is defined we always > use that and allow old-style as regular method?
exactly - and we had that before RC1-final. marcus

Andi Gutmans

22 years ago
We had a compile-error which I changed to E_STRICT :) I think we should make __construct the default if it is present. Andi At 08:42 PM 3/23/2004 +0100, Marcus Boerger wrote:

Marcus Börger

22 years ago
Hello Andi, Tuesday, March 23, 2004, 10:24:09 PM, you wrote:
> We had a compile-error which I changed to E_STRICT :) > I think we should make __construct the default if it is present.
that'd be great. marcus