[Solved] Inheriting parent methods in OO PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
xpt
Forum Newbie
Posts: 23
Joined: Tue Feb 20, 2007 1:00 pm

[Solved] Inheriting parent methods in OO PHP

Post 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')).
Last edited by xpt on Thu Feb 22, 2007 10:20 am, edited 3 times in total.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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();
?>
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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.
xpt
Forum Newbie
Posts: 23
Joined: Tue Feb 20, 2007 1:00 pm

Post by xpt »

volka wrote:base class constructors are not called automatically :(...
got it, thanks a lot!
xpt
Forum Newbie
Posts: 23
Joined: Tue Feb 20, 2007 1:00 pm

Post 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
Post Reply