Actually, it is not possible to access class methods through a variable with another method. Here there is such a class for example

class Handler { public function getHandler() { return 'some handler'; } public function getFile() { return 'some file'; } } 

I create an instance of the class $class = new Handler(); The getHandler method is available $handler = $class->getHandler();

And I want to access the getFile() method like this $file = $handler->getFile(); With this treatment, I get the error:

Fatal error: Uncaught Error: Call to a member function getFile () on string

Of course, such a call $file = $class->getFile(); it works, but I want it to work like this $file = $handler->getFile();

  • one
    Why then do you return the string? What should the getHandler method do? - vp_arth
  • one
    If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky

3 answers 3

To do this, in the getHandler method getHandler return a reference to the current object:

 public function getHandler() { // { // возможно блок каких-либо действий // } return $this; } 

Perhaps you are doing something call chains. An example with $this might look like this:

 class TestClass { public static $currentValue; private static $_instance = null; private function __construct() { } public static function getInstance() { if (self::$_instance === null) { self::$_instance = new self; } return self::$_instance; } public function toValue($value) { self::$currentValue = $value; return $this; } public function add($value) { self::$currentValue = self::$currentValue + $value; return $this; } public function subtract($value) { self::$currentValue = self::$currentValue - $value; return $this; } public function result() { return self::$currentValue; } } 

using:

 $result = TestClass::getInstance() ->toValue(5) ->add(3) ->subtract(2) ->add(8) ->result(); echo $result; // выведет 14 

This is an example. I assume that something like that you want to be used in getHandler()

  • Thank you, I wanted to implement it. - Rider_BY

For this, there are variable variables . In your case it will look like this:

 $file = $$handler->getFile(); 

And getHandler should return the name of a variable that has a getFile method.

As an example, in PHP you can call functions by name:

 $function_name = 'getHandler'; $result = $file->{$function_name}(); 

    In your example, $ handler carries the result of the getHandler () execution - string. You are trying to operate it as an instance of a class, but it is just a string (in this case) variable and does not know anything about the class, nor the class about it.

    In essence, your question is the only way:

     $handler = $class->getHandler(); $file = $class->getFile(); 
    • The question is written. Конечно такое обращение $file = $class->getFile(); работает, но я хочу, чтобы работала вот так $file = $handler->getFile(); Конечно такое обращение $file = $class->getFile(); работает, но я хочу, чтобы работала вот так $file = $handler->getFile(); :) - Aleksey Shimansky
    • I call Rasmus. Unsubscribe :) - Kirill Korushkin
    • plus 1 on the answer. wtf? - Alexey Shimansky
    • There are no prophets in their own country :) - Kirill Korushkin