there is such a class hierarchy

class Test { } // главный класс class User extends Test { function foo () { return 'func foo'; } } class Desc extends Test { function foo_desc () { // здесь нужно вызвать функцию foo из класса User } } 

It is necessary to call the methods of the related class User class from the Desc class, how to implement it?

  • Plus, always in the class, write the visibility of the method (public / private / protected). - FoxDev
  • @FoxDev, I'm aware of the visibility modifier, I need to access methods without explicitly creating an object just through $this->user - modelfak

3 answers 3

 class Test { } // главный класс class User extends Test { function foo () { return 'func foo'; } } class Desc extends Test { function foo_desc () { $u = new User; $u->foo(); } } 
  • Yes it works, but are there better methods? I know this way myself, it is necessary to implement without explicitly creating a class object, for example, to call it like this - $this->user->foo(); - modelfak
  • @modelfak is not, better methods to tighten someone else's functionality, except how to call it directly, no. You ask something like this: make it so that what lies elsewhere lies here in this, but it still lies elsewhere. - etki
  • @Etki added your answer, please look, and tell your opinion - modelfak

Of course, you can use static or initialize the class as suggested by @jedayka, but that is not the right solution. If you listen to Kent Beck , in such situations he recommends putting this method into the parent.

  • one
    added his answer as a solution - modelfak

I came up with such a solution, now I can call the methods of the next-class class using - $this

 class TEST { private $classes = array( 'pages'=>'Pages', 'users'=>'Users', ///..... other ); public static $objects = array(); public function __get($name) { if(isset(self::$objects[$name])) { return self::$objects[$name]; } if(!array_key_exists($name, $this->classes)) { return null; } $class = $this->classes[$name]; include('classes/'.$class.'.php'); self::$objects[$name] = new $class(); return self::$objects[$name]; } } 

Now inside any class that is a descendant of TEST, I can call methods of any of its other heirs

 require_once ('classes/TEST.php'); class View extends TEST { function foo () { $this->users->foo(); } 
  • You all fall when you request one class from two places. But this does not matter, because you have a problem XY: you ask for help on a specific solution, but do not say anything about the problem. - etki
  • @Etki Everything will fall when you request one class from two places - I do not understand why? - modelfak
  • because the file is locked twice and the class will be announced twice - etki
  • beat me if I'm wrong - your solution implies duplication - username
  • @Etki if(isset(self::$objects[$name])) { return self::$objects[$name]; } if(isset(self::$objects[$name])) { return self::$objects[$name]; } actually this method prevents duplication in theory - modelfak