访问兄弟方法

I've been searching for an answer all day, but I can't seem to find one. Sorry if I don't make much sense, I'm losing my mind over this.

Here's what I'm trying to do:

abstract class Parent {
    abstract public function Test();
}
class Child1 extends Parent {
    abstract public function Test() {
        echo "Test!";
    }
}
class Child2 extends Parent {
    public function Test2() {
        echo "Test2!";
        $this->Test(); // Child1::Test();
    }
}

Again, sorry if I'm not making much sense. I will try to explain more if you need me to.

You can't. What you're tryning to do has no sense in OOP. If you want to execute Test() from the first Child, you need to move the function in the parent.

EDIT: Well, I'm going to be more comprehensive:

You're trying to call a function of another child, which have only the parent class in common. Child1 and Child2 are as close as String and Integer can be in Java (or any other OOP language). Imagine that you called .upper() function on a int: it doesn't make sense.

It's not an OOP pattern, and not available in any OOP language that I kow of (except for introspection magic): Child2 do not know anything (and does not have to) about Child1 class. they could be defined independently.

I think that at this point you should explain what your needs are. I guess we could help you ,with some design pattern.

The Test overrided function in Child1 should not be abstract (as you are defining it). To call the Test function of Child1 from the Test2 function of Child2 use:

Child1 foo = new Child1();
echo foo->Test();

Thing you can do only because it's been defined as public, not because Child1 and Child2 have any relationship.

You are missing the concept of abstract classes and methods.

Abstract classes

Abstract classes are classes that define some basic behavior and cannot be instantiated. They are designed to be subclassed.

Abstract methods

These are methods that need to be implemented by the extending class. Like interfaces abstract methods define behavior without an implementation.

Your code

In your code you declared an abstract class with an abstract method.

abstract class Parent {
    public abstract function Test();
}

When another class subclasses Parent it must implement the abstract method making it concrete.

class Child extends Parent {
    public function Test() {
        echo "Test!";
    }
}

Accessing sibling method

A class or instance has no concept of a sibling. A class cannot know who subclasses its parent and access it in a static way.

There are several things wrong here:

  • Parent is a reserved word
  • Child1 contains an abstract method, so it can't contain a body.
  • Child2 extends Parent, but doesn't implement the Test() method.

As a result, none of this will instantiate. Child2() should probably extend Child1(),(or implement the Test() method itself). Correct these problems, fix any other errors that PHP might tell you about then this:

$p = new Child2();

$p->Test2();

Should give you this:

Test2!Test!