PHP/Class/Abstract Class

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

abstract class demo

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



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

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



Define abstract class

<?php
   abstract class Staff
   {
      abstract function hire();
      abstract function fire();
      abstract function promote();
      abstract function demote();
   }
?>



Using Abstract Classes and Methods

 
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("<p>%s is a %s and costs \$%.2f.</p>\n", $this->name, $this->breed, $this->price); 
    } 
    public static $type = "animal"; 
    
    public static function fly($direction = "around") { 
        printf("<p>Flying %s.</p>\n", $direction); 
    } 
    abstract public function birdCall(); 
    
} 

class Parrot extends Animal { 
    public function birdCall($singing=FALSE) { 
        $sound = $singing ? "twitter" : "chirp"; 
        printf("<p>%s says: *%s*</p>\n", $this->getName(), $sound); 
    }
}
?>



Using Abstract Classes in PHP 5

 
<?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 */
?>