RE: [PHP-DEV] __init magic method
I am actually going to +1 this idea I thought about discussing it, but opted out of it because
classes are most often in their own file and if you need to do some static binding you can do it at
the end of the file after the class definition. However, I do believe this idea would make it easier
to extend classes and make the code look much cleaner.
@Johannes In your example I'd say it would be ADBCE because I would say it should work like as
if you had each one in it's own file autoloading them. (also I believe the function would be
static)
One of the potential problems I can see is the visibility (public/private/protected) of the function
as it'd likely need to be public because it could never be auto-executed if anything else.
Software Developer
Nathan Bruer
-----Original Message-----
From: Johannes Schlüter [mailto:[email protected]]
Sent: Tuesday, January 22, 2013 7:04 AM
To: Pete Boere
Cc: [email protected]
Subject: Re: [PHP-DEV] __init magic method
Hi,
On Tue, 2013-01-22 at 12:29 +0000, Pete Boere wrote:
> Has there ever been any discussion on an __init magic method for
> bootstraping static properties on class load?
Adding an __init method as you wish will just add complexity and gain very little. What complexity
am I talking about? Well look at this single file:
<?php
class A {
public function __init() { echo __CLASS__; }
}
class B extends D {
public function __init() { echo __CLASS__; }
}
class C {
public function __init() { echo __CLASS__; }
}
class D {
public function __init() { echo __CLASS__; }
}
class E {
public function __init() { echo __CLASS__; }
}
?>
What should this print? ABCDE? Then the derived class B is initialized before the base class D,
which might have strange effects and make debugging hard. So maybe it is ACDEB? This makes the
resolution quite complicated.
On the other hand: You are showing a goos solution, which is clear and works well. PHP has the
global scope, we don't force everything into classes and then create Java-style
static{}-madness.
johannes
--
PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php
Thread (10 messages)