PHP/Class/Inheritance — различия между версиями

Материал из Web эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Текущая версия на 10:00, 26 мая 2010

Basic Inheritance

   <source lang="html4strict">

<?

   class Dog {
           public function bark( ) {
                   print "Woof!\n";
           }
   }
   class Poodle extends Dog {
   }

?>

 </source>
   
  


Class Inheritance

   <source lang="html4strict">

<?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();

?>

 </source>
   
  


Class Member Binding in PHP

   <source lang="html4strict">

<?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();

?>

 </source>
   
  


Creating a Class That Inherits from Another

   <source lang="html4strict">

<?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 ();
?>
 
 </source>
   
  


Define an Executive class that inherits Employee

   <source lang="html4strict">

<?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."
"; } } 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();

?>

 </source>
   
  


Example of Inheritance

   <source lang="html4strict">

<?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 "

The compact disk ID is: ", $mydisk->get_disk_id(), "

"; echo "

Is this a multi cd? ", ($mydisk->is_multi_cd()) ? "Yes" : "No", "

";

?>

 </source>
   
  


extends and implement

   <source lang="html4strict">

<?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."
"; } } 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."; } }

?>

      </source>
   
  


inheritance example

   <source lang="html4strict">

<?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 ( "

%s says: *chirp*

\n", $this->getName () );
 }
 public function display() {
printf ( "

%s is a %s and costs \$%.2f.

", $this->getName (), $this->getBreed (), $this->getPrice () );
 }

} class Parrot extends Bird {

 public function birdCall() {
printf ( "

%s says: *squawk*

\n", $this->getName () );
 }
 public function __construct($name) {
   parent::__construct ( $name, "parrot", 25 );
 }
 public function curse() {
printf ( "

%s

\n", $this->getName () );
 }

} ?>

 </source>
   
  


Overriding parent methods

   <source lang="html4strict">

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);
}

}

 </source>
   
  


subclass and parent class

   <source lang="html4strict">

<?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."
"; } } class Executive extends Employee { function pillageCompany() { echo "hi!"; } } $exec = new Executive(); $exec->setName("Joe"); echo $exec->getName(); $exec->pillageCompany();

?>

      </source>
   
  


Three levels of inheritance

   <source lang="html4strict">

<?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."
"; } } 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();

?>

      </source>
   
  


Using inheritance to efficiently represent various vehicle types

   <source lang="html4strict">

<? 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;
    }

} ?>

 </source>
   
  


Using the extends keyword to define a subclass

   <source lang="html4strict">

<?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.
"); }

} $fluffy=new MyCat( ); $fluffy->Birthday( ); $fluffy->sleep( ); echo "Age is $fluffy->age
"; ?>

 </source>
   
  


Using the parent construct

   <source lang="html4strict">

<?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(  );
   }

} ?>

 </source>