简体   繁体   中英

Can main classes access child class methods

If I have:

class main {
   //hello
}

class child1 {
    function love($v) {
    }
}

class child1 {
    function hate($v) {
    }
}

function __autoload($file) {
    include_once($file . '.php');
}

Is there a way I can set this up so that I can do

$main = new main();
$main->hate();
$main->love();

and still

  • keep them as seperate classes, and
  • use the autoloader for the child classes?

    I think even if the child classes extend the main class, that I can't access the child methods from the parent class. Is that correct? If so, is there something like a reverse extends which injects the child class properties/methods into the main class?


Update 1

Okay, so it looks like there's nothing straight-up built in to php to acheive this (Thanks everyone for the answers). How about I write up my goal and maybe you or someone you know can suggest a way to acheive this?

I would like to have one main class. I then have a set of subclasses which operate like grouped function libraries. Each of these subclasses is __autoload ed when needed to acheive an end. So for example, I have a subclass of file upload & processing functions, a subclass of database interaction functions, a subclass of xml processing functions, and so on.

I want to use this like: call the main class at the top of every script $main = new main(); . Then, later on, an image processing method from a child class (which has not been loaded) is needed, so I call $main->methodFromChildClass(); which will cause that child class to be autoloaded and the method used.

I am hoping in this way to optimize which files are loaded, and keep things well organized. I'm fairly new to oop. Is there a way to achieve this type of organization now?

There is not such a language construct (not in PHP anyway - other languages offer mechanisms like mixins, which kind of work like that), but there is a technique to do something like that called inversion of control.

class Main {
  public $child;

  public function __construct($child) {
    $this->child = $child;
  }

}

$main = new Main(
  new Child1()
);

$main->child->love();

This is very simplified example, that does not show full potential of this technique. For more information search for 'inversion of control' and 'design patterns'

I think you're interested in is called traits , which are not available in the current release of PHP, but will be in the next version, and is available in trunk if you check out the source from SVN and compile yourself.

For more information, see http://wiki.php.net/rfc/traits

Not to my knowledge, kinda violates the whole concept of OO programming. The main class should have the methods and properties which need to be used by all child classes of the main class, and then the child classes have methods and properties that only they will need to use.

I suppose if you really wanted to, you could store a child class object inside a main class object and do something like $main->child->hate(); but that would be sort of recursive, because if the child extends the parent class, and if the child was created and stored on the parent's construct, then you would wind up with an infinite loop of the parent creating the child which creates a new parent inside it which creates a new child which creates a new parent ad infinitum. Though you could get around that by simply having a method that would have to be manually called in order to create and store the child.

With the code you gave, it's not going to work.

If child1 extends main, you can call hate() method only by creating instance from the child1 class or by introducing the hate() method in the main() class. Child classes inherit methods from Mother classes but the reverse is not true.

What exactly are you trying to do ?

You can use magic to simulate that behaviour, but "clean" is something different

class main {
  public function __call ($name, $args) {
    switch ($name) {
      case 'hate':
        $x = new child1;
        return $x->hate();
        break;
      case 'love':
        $x = new child2;
        return $x->hate();
        break;
    }
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM