English 中文(简体)
php,我需要父母的孩子们,怎样?
原标题:php, I need child object from parent, how?
class Parent
{
 public function exec()
 {
  // here I need the child object!
 }
}

class Child extends Parent
{
 public function exec()
 {
  // something
  parent::exec();
 }
}

如你能看到的那样,我需要孩子的父母。 我怎么能做到这一点?

最佳回答

You can pass the child as an argument:

class ParentClass
{
    public function exec( $child )
    {
        echo  Parent exec ;
        $child->foo();
    }
}

class Child extends ParentClass
{
    public function exec()
    {
        parent::exec( $this );
    }

    public function foo() 
    {
        echo  Child foo ;
    }
}

很少需要这样做,因此,或许还有更好的途径来做

问题回答

暂无回答




相关问题
Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

JSON with classes?

Is there a standardized way to store classes in JSON, and then converting them back into classes again from a string? For example, I might have an array of objects of type Questions. I d like to ...

Object-Oriented Perl constructor syntax and named parameters

I m a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I ...

Passing another class amongst instances

I was wondering what is the best practice re. passing (another class) amongst two instances of the same class (lets call this Primary ). So, essentially in the constructor for the first, i can ...

Where can I find object-oriented Perl tutorials? [closed]

A Google search yields a number of results - but which ones are the best? The Perl site appears to contain two - perlboot and perltoot. I m reading these now, but what else is out there? Note: I ve ...

热门标签