The second of PHP object-oriented Programming learning

Source: Internet
Author: User

Interface

Interface is to define the common behavior of different classes, and then implement different functions within different classes. When a lot of people develop a project together, it may be to call someone else to write some of the classes, then you will ask, how do I know how the implementation of one of his functions is named, this time the PHP interface class interface play a role, when we define an interface class, The way inside it is that the following subclass must be implemented, such as:

<?phpinterface Shop  {public  function buy ($gid);  Public function sell ($gid);  Public Function view ($gid);  }  Class Baseshop implements  shop {public  function buy ($gid)  {  echo (' You have purchased the ID: '. $gid. ');  }  Public function Sell ($gid)  {  echo (' You sold the ID: '. $gid. ');  }  Public Function View ($gid)  {  echo (' You have viewed the product with ID: '. $gid. ');  }  }  ? >
Polymorphic
Because the implementation of the interface can be many, so for the interface of the top-to-top method of the specific implementation is various, this feature is called polymorphism. Polymorphism refers to the ability to redefine or change the nature and behavior of a class in object-oriented contexts based on the context in which the class is used.
PHP does not support overloading for polymorphic implementations, but PHP can be changed to achieve polymorphic effects.
Case one:

<?interface user{//User interface Public Function  getName ();p ublic function SetName ($_name);} Class Normaluser implements User {//Implement interface classes. Private $name;p ublic function GetName () {return $this->name;} Public Function SetName ($_name) {$this->name = $_name;}} Class useradmin{//operation. public static function  Changeusername (User $_user,$_username) {$_user->setname ($_ userName);}} $normalUser = new Normaluser (); Useradmin::changeusername ($normalUser, "Tom");//Here is an instance of Normaluser. Echo $normalUser->getname (); >
Case TWO:

<?php//The purpose of changing the overload of the parameter quantity by variable parameter//is not the parameter that must be passed in, must be assigned the initial value function Open_database ($DB, $cache _size_or_values=null, $ Cache_size=null) {    switch (Function_num_args ())  //Through the Function_num_args () function to calculate the number of incoming parameters, according to the number to determine the next operation    { Case        1:            $r = select_db ($DB);            break;        Case 2:            $r = select_db ($DB, $cache _size_or_values);            break;        Case 3:            $r = select_db ($DB, $cache _size_or_values, $cache _size);            break;    }    Return Is_resource ($r);}? >
Abstract class
An abstract class is somewhere between the interface and the definition of the class
PHP5 supports abstract classes and abstract methods. An abstract class cannot be instantiated directly, you must first inherit the abstract class, and then instantiate the subclass. At least one abstract method should be included in the abstract class. If a class method is declared abstract, it cannot include a specific feature implementation.
When inheriting an abstract class, the subclass must implement all the abstract methods in the abstract class, and the visibility of these methods must be the same (or looser) as in the abstract class. If an abstract method in an abstract class is declared as protected, then the methods implemented in the subclass should be declared as protected or public, not private.
<?phpabstract class abstractclass{//forcing subclasses to define these methods    abstract protected function getValue ();    Abstract protected function Prefixvalue ($prefix);    Common method (non-abstract method) public    function PrintOut () {        print $this->getvalue (). " ";    }} Class ConcreteClass1 extends abstractclass{    protected function GetValue () {        return "ConcreteClass1";    }    Public Function Prefixvalue ($prefix) {        return ' {$prefix}concreteclass1 ';    }} Class ConcreteClass2 extends abstractclass{public    function GetValue () {        return "ConcreteClass2";    }    Public Function Prefixvalue ($prefix) {        return ' {$prefix}concreteclass2 ';    }} $class 1 = new ConcreteClass1; $class 1->printout (); Echo $class 1->prefixvalue (' Foo_ '). "; $class 2 = new ConcreteClass2; $class 2->printout (); Echo $class 2->prefixvalue (' Foo_ ')." ";? >




The second of PHP object-oriented Programming learning

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.