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

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

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

abstract class demo

   <source lang="html4strict">

<?php class Employee {

   private $title;
   private $lastName;
   private $firstName;
   protected $salary;
   private $ratio = 0; 
   
   public function __construct($title, $firstName, $mainName, $salary ) { 
       $this->title     = $title;
       $this->firstName = $firstName;
       $this->lastName  = $mainName;
       $this->salary     = $salary;
   }
   public function firstName() {
       return $this->firstName;
   }
   public function getlastName() {
       return $this->lastName;
   }
   public function setRatio( $num ) {
       $this->ratio=$num;
   }
   public function getRatio() {
       return $this->ratio;
   }
   
   public function getTitle() {
       return $this->title;
   }
   public function getSalary() {
       return ($this->salary - $this->ratio);
   }
   public function getFullName() {
       return "{$this->firstName}" . " {$this->lastName}";
   }
   function getSummaryLine() {
       $base  = "$this->title ( $this->lastName, ";
       $base .= "$this->firstName )"; 
       return $base;
   }

}

abstract class EmployeeWriter {

   abstract static function write( Employee $shopProduct );

} class TextEmployeeWriter extends EmployeeWriter {

   static function write( Employee $shopEmployee ) {
       $str  = "{$shopEmployee->getTitle()}: ";   
       $str .= $shopEmployee->getFullName();
       $str .= " ({$shopEmployee->getSalary()})\n";
       print $str;
   }

} $developer1 = new Employee("A", "A1", "A2", 5.99 ); TextEmployeeWriter::write( $developer1 ); ?>


      </source>
   
  


abstract keyword is used to say that a method or class cannot be created in your program as it stands

   <source lang="html4strict">

<?

   abstract class Dog {
           abstract function bark( ) {
                   print "Woof!";
           }
   }

?>

 </source>
   
  


Define abstract class

   <source lang="html4strict">

<?php

  abstract class Staff
  {
     abstract function hire();
     abstract function fire();
     abstract function promote();
     abstract function demote();
  }

?>


      </source>
   
  


Using Abstract Classes and Methods

   <source lang="html4strict">

Any class that contains one or more abstract methods must itself be declared as abstract. An abstract class cannot be instantiated A class that extends the abstract class must implement the abstract methods of the parent class or itself be declared as abstract. <? abstract class Animal{

   function __construct($name="No-name", $breed="unknown", $price = 15) { 
       $this->name = $name; 
       $this->breed = $breed; 
       $this->price = $price; 
   } 
   function setName($name) { 
       $this->name = $name; 
   } 
   function setBreed($breed){ 
       $this->breed = $breed; 
   } 
   function setPrice($price) { 
       $this->price = $price < 0 ? 0 : $price; 
   } 
   function getName() { 
       return $this->name; 
   } 
   function display() { 
printf("

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

\n", $this->name, $this->breed, $this->price);
   } 
   public static $type = "animal"; 
   
   public static function fly($direction = "around") { 
printf("

Flying %s.

\n", $direction);
   } 
   abstract public function birdCall(); 
   

}

class Parrot extends Animal {

   public function birdCall($singing=FALSE) { 
       $sound = $singing ? "twitter" : "chirp"; 
printf("

%s says: *%s*

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

} ?>

 </source>
   
  


Using Abstract Classes in PHP 5

   <source lang="html4strict">

<?php

    abstract class Number {
         private $value;
         abstract public function value();
         public function reset() {
              $this->value = NULL;
         }
    }
    class Integer extends Number {
         private $value;
         public function value() {
              return (int)$this->value;
         }
    }
    $num = new Integer; /* Okay */
    $num2 = new Number; /* This will fail */

?>

 </source>