Has there ever been any discussion on an __init magic method for bootstraping static properties on class load?
The usual solution I come up with is manually calling an init method post class definition, which works, though I'd call it slighly less than elegant:
class Foo {
static $bar;
static function init () {
// Create $bar with expressions...
self::$bar = $bar;
}
}
Foo:init();
A custom autoloader could of course anticipate this but with systems like composer that have a shared autoload, it'd be nicer to use those than rely on a custom loader.
--
Pete Boere
Web Developer
@Pete actually, your "less than elegant" solution seems quite enough for this use case.
What's your use case for this kind of static properties? What if you make them private/protected and use an accessor method to initialize them (and avoid the overhead during autoloading)?
Marco Pivetta
What's your use case for this kind of static properties? What if you make them private/protected and use an accessor method to initialize them (and avoid the overhead during autoloading)?
It's the overhead of repeatedly calling an accessor method which I'd prefer to avoid.
--
Pete Boere
Web Developer
Hi,
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
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:johannes@php.net] Sent: Tuesday, January 22, 2013 7:04 AM
To: Pete Boere
Cc: internals@lists.php.net
Subject: Re: [PHP-DEV] __init magic method
Hi,
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
@Pete just FYI, PSR-1 excludes that such a logic should exist in a loaded file:
Not sure if you are interested in following the FIG on these ideas, but the guideline makes actually sense.
Marco Pivetta
Quoted text · 36 lines
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:johannes@php.net] Sent: Tuesday, January 22, 2013 7:04 AM
To: Pete Boere
Cc: internals@lists.php.net
Subject: Re: [PHP-DEV] __init magic methodHi,
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
--
To unsubscribe,
visit: http://www.php.net/unsub.php
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
How so?
and make the code look much cleaner.
Thankfully that's subjective. Some people prefer explicit information which can easily be read over magic, though.
@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)
In other words: It is called at completely arbitrary times. Which makes reading code hard as well as making the implementation more complex (dependency detection)
Mind that your order requires the engine to delay initialization of B and C (and E) till D is found. And then go back. That's why I listed the version with B in the end, that's a tiny bit simpler to implement, but as hard to explain.
Now for a second game: What does the following script print?
<?php
echo "A";
class foo { static function __init() { echo "B"; } }
echo "C";
?>
ABC? ACB? BAC? I guess the later as the code next to A or C might depend on the class already.
I hope you see the mess this creates, whereas we nowadays have a solution which is properly defined and can easily be understood by everybody, even by people coming from other languages and just trying to understand the code which was put in front of them.
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.
Such things would be trivial to check during compilation phase.
johannes
Quoted text · 15 lines
-----Original Message-----
From: Pete Boere [mailto:pete@the-echoplex.net] Sent: 22 January 2013 12:30
To: internals@lists.php.net
Subject: [PHP-DEV] __init magic methodHas there ever been any discussion on an __init magic method for bootstraping static properties on class load?
The usual solution I come up with is manually calling an init method post class definition, which works, though I'd call it slighly less than elegant:
class Foo {
static $bar;
static function init () {
// Create $bar with expressions...
self::$bar = $bar;
}
}
Foo:init();A custom autoloader could of course anticipate this but with systems like composer that have a shared autoload, it'd be nicer to use those than rely on a custom loader.
I'd say, that using static at all isn't elegant. There is always a way to avoid it.
Jared