The second _php tutorial of PHP Object-oriented Programming learning

Source: Internet
Author: User

The second of PHP object-oriented Programming learning


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:

 
  
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:

 
  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:

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




http://www.bkjia.com/PHPjc/953323.html www.bkjia.com true http://www.bkjia.com/PHPjc/953323.html techarticle The two interface interface of PHP object-oriented programming learning is to define the common behaviors of different classes and then implement different functions within different classes. When there are many people working together to develop an item ...

  • 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.