PHP has evolved a lot in recent years but we still can't have a static and
a nonstatic function with the same name:
class A {
function bla() { echo 'normal'; }
static function bla() { echo 'static'; }
}
A::bla();
ERROR: Cannot redeclare A::bla()
And also PHP supports calling static functions as if they were not static:
class B {
static function bla() { echo 'how come it works'; }
}
$object = new B;
$object->bla();
It confuses programmers that came from other languages, prevent APIs from
having meaningful names on static methods and have no other benefit than
supporting PHP 4 code.
I'd appreciate if anyone with sufficient knowledge/influence helped me
suggest this change to the maintainers.
IMHO, static and nonstatic functions should be "stored" in different
places. And if someone relies on this, one should use magic methods __call
and __callStatic instead.
Thanks for your time and patience,
Carlos Rodrigues
carlos@jp7.com.br
Am 28.11.2014 um 20:29 schrieb Carlos Rodrigues:
PHP has evolved a lot in recent years but we still can't have a static and
a nonstatic function with the same name:class A {
function bla() { echo 'normal'; }
static function bla() { echo 'static'; }
}A::bla();
ERROR: Cannot redeclare A::bla()
And also PHP supports calling static functions as if they were not static:
class B {
static function bla() { echo 'how come it works'; }
}$object = new B;
$object->bla();It confuses programmers that came from other languages, prevent APIs from
having meaningful names on static methods and have no other benefit than
supporting PHP 4 code.I'd appreciate if anyone with sufficient knowledge/influence helped me
suggest this change to the maintainers.IMHO, static and nonstatic functions should be "stored" in different
places. And if someone relies on this, one should use magic methods __call
and __callStatic instead.Thanks for your time and patience,
Carlos Rodrigues
carlos@jp7.com.br
Hi!
It confuses programmers that came from other languages, prevent APIs from
having meaningful names on static methods and have no other benefit than
supporting PHP 4 code.
I think this notion of "confuses programmers" sometimes goes too far,
and this is one of the examples. PHP prides itself about having low
barrier to entry, but that doesn't mean you can refuse to learn anything
at all about it and PHP should change to match that.
There's nothing wrong in having static and non-static functions in the
same namespace, and many languages do the same. Some do not, so you
should just remember PHP is the one which does.
Stas Malyshev
smalyshev@gmail.com
Hi Stanislav.
Thank you for replying. I agree that PHP can be different from other
languages, but i think you agree that a code like this just feels wrong:
class Cube {
function weird() {
echo Duck::foo();
}
}
class Duck {
public function foo() {
echo 'My class is: ' . get_class($this);
}
}
$rubik = new Cube;
$rubik->weird(); // Output: My class is: Cube
One would never guess that $this inside a method of Duck could be an
instance of Cube. It might look silly as an example. But in the real world
someone could think a method is static when its not, and call it,
introducing a bug on the application.
I don't see any case where this behavior is useful. And even it there was
one, it could be achieved using magic methods __call() and __callStatic().
Thank you all,
Carlos Rodrigues
carlos@jp7.com.br
2014-11-28 20:59 GMT-02:00 Stanislav Malyshev smalyshev@gmail.com:
Hi!
It confuses programmers that came from other languages, prevent APIs from
having meaningful names on static methods and have no other benefit than
supporting PHP 4 code.I think this notion of "confuses programmers" sometimes goes too far,
and this is one of the examples. PHP prides itself about having low
barrier to entry, but that doesn't mean you can refuse to learn anything
at all about it and PHP should change to match that.There's nothing wrong in having static and non-static functions in the
same namespace, and many languages do the same. Some do not, so you
should just remember PHP is the one which does.Stas Malyshev
smalyshev@gmail.com
Hi!
class Cube {
function weird() {
echo Duck::foo();
}}
class Duck {
public function foo() {
echo 'My class is: ' . get_class($this);
}
}$rubik = new Cube;
$rubik->weird(); // Output: My class is: Cube
This code produces:
Deprecated: Non-static method Duck::foo() should not be called
statically, assuming $this from incompatible context
which informs you about the fact that what you're doing is wrong. This
capability is kept for BC reasons. There was a discussion about dropping
this from PHP 7 but it wasn't yet decided to remove the support. In any
case, this has little to do with static and non-static functions having
different namespaces.
--
Stas Malyshev
smalyshev@gmail.com
Hi,
Glad to hear it was discussed. I understand you guys are busy and can't
just change things out of nowhere, but i wish it will be dropped in the
future.
It it gets dropped in the future i will come back and ask to make it
possible to call a static method and normal methods with the same name :P.
For now i will just wait.
Thx anyway,
Carlos Rodrigues
2014-12-01 15:25 GMT-02:00 Stanislav Malyshev smalyshev@gmail.com:
Hi!
class Cube {
function weird() {
echo Duck::foo();
}}
class Duck {
public function foo() {
echo 'My class is: ' . get_class($this);
}
}$rubik = new Cube;
$rubik->weird(); // Output: My class is: CubeThis code produces:
Deprecated: Non-static method Duck::foo() should not be called
statically, assuming $this from incompatible contextwhich informs you about the fact that what you're doing is wrong. This
capability is kept for BC reasons. There was a discussion about dropping
this from PHP 7 but it wasn't yet decided to remove the support. In any
case, this has little to do with static and non-static functions having
different namespaces.--
Stas Malyshev
smalyshev@gmail.com
It it gets dropped in the future i will come back and ask to make it
possible to call a static method and normal methods with the same name :P.
For now i will just wait.
Can’t be done. It’s important for inheritance that you can call methods of the parent class, which your suggestion would prevent.
Andrea Faulds
http://ajf.me/