PHP permission statement question

I'm having problems with the method call style MyClass::function();and can't figure out why. Here is an example (I am using the Kohana btw framework):

    class Test_Core
 {
  public $var1 = "lots of testing";

  public function output()
   {
    $print_out = $this->var1;
    echo $print_out;
   }
 }

I am trying to use the following to call it, but it returns $ var1 as undefined:

Test::output()

However, this works great:

  $test = new Test(); 
  $test->output();

I usually use this style of calling objects as opposed to the style of the "new class", but I cannot understand why it does not want to work.

+3
source share
4 answers

Using this:

Test::output()

You call your method as static, and static methods do not have access to instance properties, since there is no instance.

, , , .


, :


, :

- $this .

:

E_STRICT.

+4

You cannot use $ this during a static call because $ this refers to an object that was not created in your case.

0
source

Try Test_Core::output()it because you are using the wrong class name

-1
source

Source: https://habr.com/ru/post/1739628/


All Articles