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
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php