PHP/Class/Inheritance
Содержание
- 1 Basic Inheritance
- 2 Class Inheritance
- 3 Class Member Binding in PHP
- 4 Creating a Class That Inherits from Another
- 5 Define an Executive class that inherits Employee
- 6 Example of Inheritance
- 7 extends and implement
- 8 inheritance example
- 9 Overriding parent methods
- 10 subclass and parent class
- 11 Three levels of inheritance
- 12 Using inheritance to efficiently represent various vehicle types
- 13 Using the extends keyword to define a subclass
- 14 Using the parent construct
Basic Inheritance
<?
class Dog {
public function bark( ) {
print "Woof!\n";
}
}
class Poodle extends Dog {
}
?>
Class Inheritance
<?php
class ParentClass {
public $parentvar;
public function parentOne() {
echo "Called parentOne()\n";
}
private function parentTwo() {
echo "Called parentTwo()!\n";
}
}
class ChildClass extends ParentClass {
public function childOne() {
echo "Called childOne()!\n";
}
}
$v = new ChildClass();
$v->parentOne();
?>
Class Member Binding in PHP
<?php
class ParentClass {
public function callMe() {
$this->anotherCall();
}
public function anotherCall() {
echo "Parent called!\n";
}
}
class ChildClass extends ParentClass {
public function anotherCall() {
echo "Child called!\n";
}
}
$child = new ChildClass;
$child->callMe();
?>
Creating a Class That Inherits from Another
<?php
class Item {
var $name;
function Item( $name="item", $code=0) {
$this->name = $name;
$this->code = $code;
}
function getName() {
return $this->name;
}
}
class PriceItem extends Item {
}
$item = new PriceItem( "widget", 5442 );
print $item->getName ();
?>
Define an Executive class that inherits Employee
<?php
class Employee {
private $name;
function setName($name) {
if ($name == "") echo "Name cannot be blank!";
else $this->name = $name;
}
function getName() {
return "My name is ".$this->name."<br />";
}
}
class Executive extends Employee {
function pillageCompany() {
echo "I"m selling company assets to finance my yacht!";
}
}
$exec = new Executive();
$exec->setName("R");
echo $exec->getName();
$exec->pillageCompany();
?>
Example of Inheritance
<?php
class cd {
public $artist;
public $title;
protected $tracks;
private $disk_id;
public function __construct() {
$this->disk_id = sha1("cd" . time() . rand());
}
public function get_disk_id() {
return $this->disk_id;
}
}
class cd_album extends cd {
protected $num_disks;
public function __construct($disks = 1) {
$this->num_disks = $disks;
parent::__construct();
}
public function is_multi_cd() {
return ($this->num_disks > 1) ? true : false;
}
}
$mydisk = new cd_album(3);
echo "<p>The compact disk ID is: ", $mydisk->get_disk_id(), "</p>";
echo "<p>Is this a multi cd? ",
($mydisk->is_multi_cd()) ? "Yes" : "No",
"</p>";
?>
extends and implement
<?php
class Employee {
private $name;
function setName($name) {
if ($name == "")
echo "Name cannot be blank!";
else
$this->name = $name;
}
function getName() {
return "My name is ".$this->name."<br />";
}
}
interface MyInterface
{
function account();
function doc();
}
class Executive extends Employee implements MyInterface
{
private $totalStockOptions;
function account()
{
echo "account";
}
function doc()
{
echo "doc";
}
}
class Assistant extends Employee implements MyInterface
{
function takeMemo() {
echo "memo";
}
function account(){
echo "memo";
}
function doc(){
echo "Start small fire in the trash can.";
}
}
?>
inheritance example
<?php
class Bird {
private $name;
private $breed;
private $price;
public function __construct($name, $breed, $price = 15) {
$this->setName ( $name );
$this->setBreed ( $breed );
$this->setPrice ( $price );
}
public function birdCall() {
printf ( "<p>%s says: *chirp*</p>\n", $this->getName () );
}
public function display() {
printf ( "<p>%s is a %s and costs \$%.2f.</p>", $this->getName (), $this->getBreed (), $this->getPrice () );
}
}
class Parrot extends Bird {
public function birdCall() {
printf ( "<p>%s says: *squawk*</p>\n", $this->getName () );
}
public function __construct($name) {
parent::__construct ( $name, "parrot", 25 );
}
public function curse() {
printf ( "<p>%s</p>\n", $this->getName () );
}
}
?>
Overriding parent methods
class DB {
public $result;
function getResult() {
return $this->result;
}
function query($sql) {
error_log("query() must be overridden by a database-specific child");
return false;
}
}
class MySQL extends DB {
function query($sql) {
$this->result = mysql_query($sql);
}
}
subclass and parent class
<?php
class Employee {
private $name;
function setName($name) {
if ($name == "")
echo "Name cannot be blank!";
else
$this->name = $name;
}
function getName() {
return "My name is ".$this->name."<br />";
}
}
class Executive extends Employee {
function pillageCompany() {
echo "hi!";
}
}
$exec = new Executive();
$exec->setName("Joe");
echo $exec->getName();
$exec->pillageCompany();
?>
Three levels of inheritance
<?php
class Employee {
private $name;
function setName($name) {
if ($name == "") echo "Name cannot be blank!";
else $this->name = $name;
}
function getName() {
return "My name is ".$this->name."<br />";
}
}
class Executive extends Employee {
function methodB() {
echo " my yacht!";
}
}
class CEO extends Executive {
function methodC() {
echo "tuck";
}
}
$ceo = new CEO();
$ceo->setName("Joe");
$ceo->methodB();
$ceo->methodC();
?>
Using inheritance to efficiently represent various vehicle types
<?
class Vehicle {
var $model;
var $current_speed;
function setSpeed($mph) {
$this->current_speed = $mph;
}
function getSpeed() {
return $this->current_speed;
}
}
class Auto extends Vehicle {
var $fuel_type;
function setFuelType($fuel) {
$this->fuel_type = $fuel;
}
function getFuelType() {
return $this->fuel_type;
}
}
class Airplane extends Vehicle {
var $wingspan;
function setWingSpan($wingspan) {
$this->wingspan = $wingspan;
}
function getWingSpan() {
return $this->wingspan;
}
}
?>
Using the extends keyword to define a subclass
<?php
class Cat {
var $age;
function Cat($new_age){
$this->age = $new_age;
}
function Birthday( ){
$this->age++;
}
}
class MyCat extends Cat {
function MyCat( ) {
}
function sleep( ) {
echo("Zzzzzz.<br />");
}
}
$fluffy=new MyCat( );
$fluffy->Birthday( );
$fluffy->sleep( );
echo "Age is $fluffy->age <br />";
?>
Using the parent construct
<?php
class Cat {
var $age;
function Cat($new_age){
$this->age = $new_age;
}
function Birthday( ){
$this->age++;
}
function Eat( ){
echo "Chomp chomp.";
}
function Meow( ){
echo "Meow.";
}
}
class MyCat extends Cat {
function MyCat( ) {
}
function eat( ) {
parent::eat( );
$this->meow( );
}
}
?>