# Static functions Vs Nonstatic functions

- Thread: https://externals.io/message/79289
- Original archive: https://news-web.php.net/php.internals/79289
- Started by Carlos Rodrigues on 2014-11-28T19:29:37+00:00
- Messages: 7
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #79289 — Carlos Rodrigues — 2014-11-28T19:29:37+00:00

URL: https://externals.io/message/79289#79289

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

---

## #79294 — Marc Bennewitz — 2014-11-28T20:50:38+00:00

URL: https://externals.io/message/79289#79294 · In reply to: #79289

Please read : 
http://grokbase.com/t/php/php-internals/14acs23cpe/disallow-non-static-method-calls-with-self-static-in-php-7#20141026ct1np9k82bte2m9k9rtmq1c2jr

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

---

## #79298 — Stanislav Malyshev — 2014-11-28T22:59:39+00:00

URL: https://externals.io/message/79289#79298 · In reply to: #79289

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

---

## #79336 — Carlos Rodrigues — 2014-12-01T12:26:29+00:00

URL: https://externals.io/message/79289#79336 · In reply to: #79298

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!
>
> > […]
>
> 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

---

## #79350 — Stanislav Malyshev — 2014-12-01T17:25:50+00:00

URL: https://externals.io/message/79289#79350 · In reply to: #79336

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

---

## #79354 — Carlos Rodrigues — 2014-12-01T19:48:15+00:00

URL: https://externals.io/message/79289#79354 · In reply to: #79350

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!
>
> > […]
>
> 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

---

## #79357 — Andrea Faulds — 2014-12-01T20:42:00+00:00

URL: https://externals.io/message/79289#79357 · In reply to: #79354

> 
> 
> 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/
