PHP Classes: Parent / Child Relationship

I am having problems extending classes in PHP. Have been Google for a while.

 $a = new A();
 $a->one();

 $a->two();
 // something like this, or...

 class A {
  public $test1;

   public function one() {
    echo "this is A-one";

    $this->two();
    $parent->two();
    $parent->B->two();
    // ...how do i do something like this (prepare it for using in instance $a)?

   }
 }

 class B extends A {
   public $test2;

    public function two($test) {
     echo "this is B-two";

    }
 }

      

I'm fine with PHP.

+2


a source to share


4 answers


Your examples are fine, but you see a little confusion here:

public function one() {
    echo "this is A-one";

    $this->two();
    $parent->two();
    $parent->B->two();
}

      

what do you want i think:

class A
{
    function one()
    {
        echo "A is running one\n";
        $this->two();
    }
    function two()
    {
        echo "A is running two\n";
    }

}

class B extends A
{
    function two()
    {
        echo "B is running two\n";
    }
}

      

Then you want to create an object of type B and call function "one"



$myB = new B();
$b->one();

      

This will lead to the conclusion

A is running one
B is running two

      

This is an example of the behavior of a polymorphic class. The superclass will know to call the current version of the function instance "two". This is a standard feature in PHP and most object oriented languages.

Note that the superclass never knows about subclasses, the only reason you can call method "two" and run the B version is because function "two" was defined in the parent class (A).

+2


a source


It's impossible. First, class A is the parent of class B, so using anything with the parent is right in the list.

There are a number of things that are relevant to the child class that are not appropriate for the parent class:



  • Class B requires A to operate
  • Class B can do all A plus plus more
  • Class B has access (as far as it is allowed to access) all data of class A

None of these things are true in reverse, so together they make up the reason why you cannot call the child function.

+2


a source


Read the section on Object Inheritance in the PHP manual carefully . Yes, there's a lot of information out there at http://us2.php.net/oop , but that might help you think about what you can get from OOP .

0


a source


here's what you can do:

class A{
  public function methodOfA (){
    echo "this is a method of A (and therefore also of B)";
  }
}

class B extends A{
  public function methodOfB (){
    echo "this is a method of B";
    // you can do {$this->methodOfA ()} if you want because all of A is inherited by B
  }
}

$a = new A ();  // $a is an A
$a->methodOfA ();     // this is OK because $a is an A
// can't do {$a->methodOfB ()} because $a is not a B

$b = new B ();  // $b is a B, and it is also an A, because B extends A
$b->methodOfB (); // ok because $b is a B
$b->methodOfA (); // ok becuase $b is an A

      

Of course there is much more there. There's a nice OOP section in the php manual (in artlung's answer).

0


a source







All Articles