PHP Inheritance

It allows you to create a new class that reuses the properties and methods from an existing class. A class that inherits from another class is called subclass (also a child class).


<?php

class Fruits{

	public $name;

	public function setName($name){
		$this->name = $name;
	}
}

class Apple extends Fruits{

	function juice(){
		echo "Juice <br/>";
	}
}


class Mango extends Fruits{

	
}

$apple = new Apple();
$apple->juice();
$apple->setName("My Apple");
echo $apple->name;

echo "<br/>";

$mango = new Mango();
$mango->setName("My Mango");
echo $mango->name;

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s