Object-oriented features of PHP OOP

Source: Internet
Author: User
Tags php oop lenovo

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 {

Field (member) of the class

Private $_name = ' Lenovo 120 ';

Private $_model = ' LX ';

}

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

Class Computer {

Field (member) of the class

Private $_name = ' Lenovo 120 ';

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 {

Field (member) of the class

Private $_name;

Private $_model;

Assign value

function SetName ($_name) {

$this->_name = $_name;

}

Take 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 {

Field (member) 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 {

Field (member) 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 {

Const PI = 3.1415926;

}

echo Computer::P i;

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 {

public static $_count = 0;

}

Echo Computer::$_count;

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

Class Computer {

private static $_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 {

//

}

$computer = new 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 120 ';

Private Function __get ($_key) {

return $this->$_key;

}

Public Function run () {

Echo ' I am the parent ';

}

}

Class Notebookcomputer 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 120 ';

protected function Run () {

Echo ' I am the parent ';

}

}

Class Notebookcomputer extends Computer {

Public $_name = ' IBM ';

Public Function run () {

Echo ' I am a sub-class ';

}

}

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 120 ';

protected function Run () {

Echo ' I am the parent ';

}

}

Class Notebookcomputer extends Computer {

Public Function 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 {

protected function Run () {

Echo ' I am the parent ';

}

}

Class Notebookcomputer extends Computer {

Public Function 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 {

Classes that cannot be inherited

Final public Function run () {}//method cannot be inherited

}

Class Notebookcomputer extends 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 {

Abstract function run ();

}

Final class Notebookcomputer extends computer {

Public Function run () {

Echo ' I have fulfilled ';

}

}

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 120 ';

Public function run ();

}

Final class Notebookcomputer implements computer {

Public Function 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 120 ';

Public function run ();

}

Interface Notebook {

Public function book ();

}

Final class Notebookcomputer implements Computer,notebook {

Public Function run () {

Echo ' implements the method of the interface ';

}

Public Function Book () {

Echo ' implements the method of the interface ';

}

}

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.

Interface Computer {

Public function version ();

Public function work ();

}

Class Notebookcomputer implements computer {

Public Function version () {

Echo ' Lenovo 120 ';

}

Public function work () {

The echo ' notebook is being carried at any time! ‘;

}

}

Class Desktopcomputer implements computer {

Public Function version () {

Echo ' IBM ';

}

Public function work () {

Echo ' desktop computer is running on the workstation! ‘;

}

}

Class Person {

Public function Run ($type) {

$type->version ();

$type->work ();

}

}

$person = new Person ();

$desktopcomputer = new Desktopcomputer ();

$notebookcomputer = new Notebookcomputer ();

$person->run ($notebookcomputer);


Object-oriented features of PHP OOP

Related Article

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.