A
A
alestro2015-10-15 15:18:10
PHP
alestro, 2015-10-15 15:18:10

A static call to a non-static method fires, how so?

There are non-static methods - test1 and test2
When they are called statically, they work as static ones, what's the catch?

class Test {
  
  public function test(){
    echo 'ты меня видишь';
  }
  public function test2(){
    echo 12;
  }
}

$d = Test::test1(); // выводит: ты меня видишь
$a = Test::test2(); // выводит: 12

Answer the question

In order to leave comments, you need to log in

4 answer(s)
S
shagguboy, 2015-10-15
@alestro

fixed in php 7. before that it was BC BREAK

A
Alexander Varakosov, 2015-10-15
@thelongrunsmoke

This is PHP.

R
Rikcon, 2015-10-15
@Rikcon

sandbox.onlinephpfunctions.com
checked your code.
It throws a Warning, but the code executes.
Such is our PHP.

<br />
<b>Strict Standards</b>:  Non-static method Test::test1() should not be called statically in <b>[...][...]</b> on line <b>11</b><br />
ты меня видишь<br />
<b>Strict Standards</b>:  Non-static method Test::test2() should not be called statically in <b>[...][...]</b> on line <b>12</b><br />
12

I
Ivan Koryukov, 2015-10-15
@MadridianFox

The method does not use object data, so nothing prevents it from being called statically. If inside there was a call to the field through $this, then an error would pop up.
And yes, it's PHP...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question