简体   繁体   English

主类可以访问子类方法

[英]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. 认为即使子类extend了主类,我也无法从父类访问子方法。 Is that correct? 那是对的吗? If so, is there something like a reverse extends which injects the child class properties/methods into the main class? 如果是这样,是否有类似反向extends东西将子类属性/方法注入主类?


Update 1 更新1

Okay, so it looks like there's nothing straight-up built in to php to acheive this (Thanks everyone for the answers). 好吧,所以看起来没有什么直接内置到PHP来实现这一点(谢谢大家的答案)。 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. 当需要实现结束时,这些子类中的每一个都是__autoload 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. 例如,我有一个文件上传和处理函数的子类,一个数据库交互函数的子类,一个xml处理函数的子类,等等。

I want to use this like: call the main class at the top of every script $main = new main(); 我想使用它:在每个脚本的顶部调用主类$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(); 然后,稍后,需要来自子类(尚未加载)的图像处理方法,因此我调用$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. 我对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. 没有这样的语言结构(不管是在PHP中 - 其他语言提供像mixin这样的机制,这类似的工作),但是有一种技术可以做类似于控制反转的操作。

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. 我认为你感兴趣的是被称为traits ,这在当前版本的PHP中是不可用的,但是将在下一个版本中,如果从SVN查看源代码并自行编译,则可以在trunk中使用。

For more information, see http://wiki.php.net/rfc/traits 有关更多信息,请访问http://wiki.php.net/rfc/traits

Not to my knowledge, kinda violates the whole concept of OO programming. 据我所知,有点违反OO编程的整个概念。 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(); 我想如果你真的想要,你可以在一个主类对象中存储一个子类对象,并执行类似$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. 如果child1扩展main,则只能通过从child1类创建实例或在main()类中引入hate()方法来调用hate()方法。 Child classes inherit methods from Mother classes but the reverse is not true. 子类继承Mother类的方法,但反之则不然。

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;
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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