18th. Object-oriented features _php tutorial

Source: Internet
Author: User
Tags lenovo
Learning Essentials:
1.OOP Package
Inheritance of 2.OOP
Polymorphism of 3.OOP

The three main features of object-oriented are encapsulation, inheritance, and polymorphism.

A The encapsulation of OOP

Hides the fields and implementation details of the object, exposes the interface only externally, and controls the access level of the Read and modified fields in the program
Abstract data and behavior (or function) to form an organic whole, that is, the data and
The source code for manipulating data is organically combined to form "classes" where data and functions are members of the class.

Scope of field
1.public public (out-of-class accessible)
2.private Private (can be accessed within the class)
3.protected protected (within-class and subclass-accessible, outside-class inaccessible)

Creates a field that uses a private, so that the external cannot access the

class Computer {    /// class field (member)    private$_name = ' Lenovo '  ;     Private $_model = ' LX ';}

Using a public method as the portal, access to the private field, and must use the $this keyword.

class Computer {    /// class field (member)    private$_name = ' Lenovo ';     Private $_model = ' LX ';     // accessing private fields    through public methods function run () {        echo$this-_name;    }} $computer->run ();

Attribute operation (Assignment and value of private fields)

Two public methods can be designed, one method is SetName (), which is used to assign values, one method is GetName (),
Used to take a value.

class computer     fields (members) of the{// Class)    private$_name;     Private $_model ;     // Assign Value    function setName ($_name) {        $this$_name;    }     // value    function  getName () {        return$ this, _name;    }} $computer New computer (); $computer->setname (' IBM ' ); Echo $computer->getname ();

If there are 10 fields then there must be 20 methods to be able to assign values and values, then there is no easier way
It? PHP built-in two methods (interceptors) specifically for the value and assignment: __set (), __get ().

class computer     fields (members) of the{// Class)    private$_name;     Private $_model ;     // All fields are assigned here    function __set ($_key$_value) {        $this-$_key  $_value;    }     // The values for    All fields are taken here function __get ($_key) {        return$this-$_key ;    }} $computer New computer (); $computer->_model = ' LX '; Echo $computer->_model;

Method Private: Some of the methods used in the class do not need to be public, just part of the operation inside, this time
The method can also be encapsulated.

class computer     fields (members) of the{// Class)    private$_name;     Private $_model ;     // Private Methods    Private function Getecho () {        echo ' I am the method of privatization'    ;    } // public methods are generally external entrances.     Public function run () {        $this-Getecho ();    }} $computer New computer (); $computer->run ();

Recommendation: If there is no modifier in front of the method, then it is an externally accessible public method, but in order to make the program more
, it is recommended to add public to the front.

Constants (constant)
Constants can be defined in a class to represent values that do not change. For any object instantiated from this class, it is often
The measures remain constant throughout the life cycle of these objects.

class Computer {    constPI = 3.1415926;} Echo Computer::PI;

Static Class Members
Sometimes, you might need to create fields and methods for all class instances to share, and these fields and methods are
example, but cannot be called by any particular object.

class Computer {    publicstatic$_count = 0;} Echo Computer::$_count;

In general, fields must be privatized. So you might want to do this:

class Computer {    privatestatic$_count = 0;      Public Static function Setrun () {        self::$_count ++    ;    }  Public Static function Getrun () {        return self::$_count;    }} Computer::setrun (); Echo Computer::getrun ();

instanceof Keywords
PHP5 has a instanceof keyword that uses this keyword to determine whether an object is an instance of a class, a class
Subclass, or implement a specific interface, and perform the appropriate operation.

class computer {    }$computernew  computer (); Echo ($computer instanceof Computer);

Two OOP inheritance

Inheritance is the mechanism of getting one or more classes from a base class.
A class that inherits from another class is called a subclass of that class. This relationship is usually likened to the parent class and the child. Sub-class will continue to
The attributes of the parent class. These attributes are made up of properties and methods. Subclasses can add new functionality outside of the parent class, so subclasses Also
is called the "extension" of the parent class.

In PHP, class inheritance is implemented by the extends keyword. Classes that inherit from other classes become subclasses or derived classes, child
Class is a parent class or a base class that inherits classes. (PHP only supports single inheritance, PHP does not support method overloading).

class Computer {    private$_name = ' Lenovo ';     Private function __get ($_key) {        return$this-$_key ;    }      Public function run () {        echo ' I am the parent ';    }} class extends Computer {} $notebookcomputer New Notebookcomputer (); $notebookcomputer, run (); Echo $notebookcomputer->_name;

Overrides for fields and methods (Overrides)
Sometimes, the parent class's fields and methods are not particularly needed, so you can modify the parent class by overriding the subclass.
Fields and methods.

class Computer {    public$_name = ' Lenovo '    ; protected function run () {        echo ' I am the parent ';    }} class extends Computer {    public$_name = ' IBM ';      Public function run () {        echo ' I am a subclass ';    }}

A child class invokes a field or method of a parent class
In order to be secure, we generally encapsulate the methods of the parent class so that the external cannot be called and can only be inherited.
As seen by the subclass. At this point, you need to invoke the parent class through the subclass operation.

class Computer {    protected$_name = ' Lenovo ';     protected function run () {        echo ' I am the parent ';    }} class extends Computer {    publicfunction  getName () {        echo$ this, _name;    }      Public function Getrun () {        echo$this-run ();    }}

Calling a method of a parent class by overriding
Sometimes we need to be able to invoke the method content of the parent class through the overridden method, which must be used
Syntax: The parent class Name:: Method () or Parent:: Method () can be called.

class Computer {    protectedfunction  run () {        echo ' I am the parent class ';    }} class extends Computer {    publicfunction  run () {        echo computer:: run ();}    }

The final keyword prevents the class from being inherited, and sometimes it just wants to be a separate class and not be inherited by other classes.
Then you must use this keyword. It is recommended to add this keyword as long as it is a separate class.

Final class Computer {    // cannot inherit the class    finalpublicfunction  Run () {    // cannot be inherited method }classextends Computer {    // will error }

Abstract classes and methods
Abstract methods are special, only declared in the parent class, but implemented in subclasses. Only classes declared as abstract can sound
Abstract methods.

Rules:
1. Abstract classes cannot be instantiated and can only be inherited.
2. Abstract methods must be overridden by the quilt class method.

Abstract class Computer {    abstractfunction  run ();} Final class extends Computer {    publicfunction  run () {        echo ' I realized ' ;    }}

Interface (interface)
The interface defines a general specification for implementing a service, declares the required functions and constants, but does not specify how to implement them.
The reason for not giving implementation details is that different entities may need to implement public methods in different ways
Yi. The key is to establish a set of general principles that must be achieved, so long as these principles are met, the interface can be realized.

Rules:
1. Classes are all abstract (no need to declare abstract)
2. The interface abstraction method must be public
3. The member (field) must be a constant

Interface Computer {    const NAME = ' Lenovo '    ;  Public function run ();} Final class Implements Computer {    publicfunction  run () {        echo ' implements the method of the interface ' ;    }} $notebookcomputer New Notebookcomputer (); $notebookcomputer, run (); Echo Computer::name;

Subclasses can implement multiple interfaces

Interface Computer {    const NAME = ' Lenovo '    ;  Public function run ();} Interface Notebook {    publicfunction book ();} Final class Implements Computer, Notebook {    publicfunction  run () {        echo ') The method of implementing the interface'    ;    }  Public function Book () {        echo ' implements the interface method ';    }}

Three Polymorphic

Polymorphism refers to the ability of OOP to redefine or change the nature or behavior of a class based on the context of the class being used, or
Many different implementations of the port are polymorphic. Different subclass objects are treated as parent classes, and different subclasses can be masked
The differences between objects, write generic code, and make generic programming to suit the changing needs.

InterfaceComputer { Public functionversion ();  Public functionWork ();}classNotebookcomputerImplementsComputer { Public functionversion () {Echo' Lenovo 120 '; }     Public functionWork () {Echo' Notebooks are being carried at any time! '; }}classDesktopcomputerImplementsComputer { Public functionversion () {Echo' IBM '; }     Public functionWork () {Echo' The desktop computer is running on the workstation! '; }}classPerson { Public functionRun$type) {        $type-version (); $type-Work (); }}$person=NewPerson ();$desktopcomputer=NewDesktopcomputer ();$notebookcomputer=NewNotebookcomputer ();$person->run ($notebookcomputer);

Note: The article is from Eon to PHP video tutorial, this article is limited to exchange use, not for commercial purposes, otherwise the consequences.

http://www.bkjia.com/PHPjc/776507.html www.bkjia.com true http://www.bkjia.com/PHPjc/776507.html techarticle Learning Essentials: 1.OOP Package 2.OOP Inheritance 3.OOP's three main features of polymorphic object-oriented are encapsulation, inheritance, and polymorphism. A Oop encapsulates the fields and implementation details of hidden objects ...

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