Page 1 of 1

[Solved] Inheriting parent methods in OO PHP

Posted: Thu Feb 22, 2007 9:57 am
by xpt
How to inherit parent method in PHP?

Giving the following class for example (from http://swik.net/PHP/Object+Oriented+PHP),

Code: Select all

Class HelloWorld
{
  public $word;

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

  public function print()
  {
    echo $this->word;
  }
}

class HelloWorld2 extends HelloWorld
{
}

$hello = new HelloWorld2('hello world');

$hello->print();
I want that in the HelloWorld2 class, inherit the parent HelloWorld methods. E.g., in construction, call parent construction first then add '\nand more' to $word. In print(), having called parent print() method, echo another '\ndone'.

So the final output will be:

hello world
and more
done

How could I do that?

thanks a lot

Got partial answer from
http://www.sitepoint.com/article/object-oriented-php/8

[...]

Please show me how to do the cascading constructors.

Code: Select all

Class HelloWorld
{
  public $word;

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

  public function dump()
  {
    echo $this->word;
  }
}

class HelloWorld2 extends HelloWorld
{
  public function __construct($word)
 {
    parent::__constructor($word);
    $this->word .= "\nand";
 }
  public function dump()
  {
    parent::dump();
   echo '\ndone';
  }

}
I've updated your code to show how to call the parent constructor & expand on the $word variable. print() is a built-in PHP function, so I renamed the function to dump() (untested, but I'm not sure how happy PHP would be to have an object function be the same name as a built-in function. At the very least you should stay away from it in case you type print('blah') when you were really meaning $this->print('blah')).

Posted: Thu Feb 22, 2007 10:05 am
by volka
base class constructors are not called automatically :(
print is a keyword

Code: Select all

<?php
class HelloWorld
{
  public $word;

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

  public function foo()
  {
    echo $this->word;
  }
}

class HelloWorld2 extends HelloWorld
{
	public function __construct($word) {
		parent::__construct($word);
	}
}

$hello = new HelloWorld2('hello world');

$hello->foo();
?>

Posted: Thu Feb 22, 2007 10:06 am
by Chris Corbyn

Code: Select all

Class HelloWorld
{
  public $word;

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

  public function print()
  {
    echo $this->word;
  }
}

class HelloWorld2 extends HelloWorld
{
    public function __construct()
    {
        parent::__construct();
        $this->word .= "\nand more";
    }
    
    public function print()
    {
       parent::print();
       echo "\nand done";
    }
}

$hello = new HelloWorld2('hello world');

$hello->print();
EDIT | Volka has a point, print is a keyword.

Posted: Thu Feb 22, 2007 10:08 am
by xpt
volka wrote:base class constructors are not called automatically :(...
got it, thanks a lot!

Posted: Thu Feb 22, 2007 10:12 am
by xpt
[quote="xpt"][/quote]

Note to myself (and might help other newbies)
PHP Application Design Part 1: OOP In Full Effect
http://www.phpfreaks.com/tutorials/150/1.php