Home >Backend Development >PHP Tutorial >Chapter 18 Object-oriented Features_PHP Tutorial

Chapter 18 Object-oriented Features_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 10:29:22923browse

Learning points:
1. OOP encapsulation
2. OOP inheritance
3 .OOP polymorphism

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

1. OOP encapsulation

Hide the fields and implementation details of the object, only expose the interface to the outside world, and control the access level of reading and modifying the fields in the program
; combine the abstracted data and behaviors (or functions) to form an organic The whole, that is, the data is organically combined with the source code of
to operate the data to form a "class", in which data and functions are both members of the class.

Field scope
1.public (accessible outside the class)
2.private (accessible within the class)
3.protected (Accessible within the class and subclasses, but not accessible outside the class)

Create a private field so that it cannot be accessed by the outside world

<span>class</span><span> Computer {
    </span><span>//</span><span>类的字段(成员)</span>
    <span>private</span> <span>$_name</span> = '联想120'<span>;
    </span><span>private</span> <span>$_model</span> = 'LX'<span>;
}</span>

Use a public method as the entrance to access private fields, and you must use the $this keyword.

<span>class</span><span> Computer {
    </span><span>//</span><span>类的字段(成员)</span>
    <span>private</span> <span>$_name</span> = '联想120'<span>;
    </span><span>private</span> <span>$_model</span> = 'LX'<span>;
    </span><span>//</span><span>通过公共方法来访问私有字段</span>
    <span>function</span><span> run() {
        </span><span>echo</span> <span>$this</span>-><span>_name;
    }
}
</span><span>$computer</span>->run ();

Attribute operations (assignment and value acquisition of private fields)

You can design two public methods, one method is setName(), used for assigning values; the other method is getName(),
is used for obtaining values.

<span>class</span><span> Computer {
    </span><span>//</span><span>类的字段(成员)</span>
    <span>private</span> <span>$_name</span><span>;
    </span><span>private</span> <span>$_model</span><span>;
    </span><span>//</span><span>赋值</span>
    <span>function</span> setName(<span>$_name</span><span>) {
        </span><span>$this</span>->_name = <span>$_name</span><span>;
    }
    </span><span>//</span><span>取值</span>
    <span>function</span><span> getName() {
        </span><span>return</span> <span>$this</span>-><span>_name;
    }
}
</span><span>$computer</span> = <span>new</span><span> Computer ();
</span><span>$computer</span>->setName ( 'IBM'<span> );
</span><span>echo</span> <span>$computer</span>->getName ();

If there are ten fields, then twenty methods must be used to assign and obtain values. Is there an easier method
? PHP has two built-in methods (interceptors) dedicated to value acquisition and assignment: __set(), __get().

<span>class</span><span> Computer {
    </span><span>//</span><span>类的字段(成员)</span>
    <span>private</span> <span>$_name</span><span>;
    </span><span>private</span> <span>$_model</span><span>;
    </span><span>//</span><span>所有字段的赋值都在这里进行</span>
    <span>function</span> __set(<span>$_key</span>, <span>$_value</span><span>) {
        </span><span>$this</span>-><span>$_key</span> = <span>$_value</span><span>;
    }
    </span><span>//</span><span>所有字段的取值都在这里进行</span>
    <span>function</span> __get(<span>$_key</span><span>) {
        </span><span>return</span> <span>$this</span>-><span>$_key</span><span>;
    }
}
</span><span>$computer</span> = <span>new</span><span> Computer ();
</span><span>$computer</span>->_model = 'LX'<span>;
</span><span>echo</span> <span>$computer</span>->_model;

Private methods: Some methods in the class do not need to be exposed to the outside world, but are just part of the operation inside. At this time,
the methods can also be encapsulated.

<span>class</span><span> Computer {
    </span><span>//</span><span>类的字段(成员)</span>
    <span>private</span> <span>$_name</span><span>;
    </span><span>private</span> <span>$_model</span><span>;
    </span><span>//</span><span>私有方法</span>
    <span>private</span> <span>function</span><span> getEcho() {
        </span><span>echo</span> '我是私有化的方法'<span>;
    }
    </span><span>//</span><span>公共方法一般是对外的入口</span>
    <span>public</span> <span>function</span><span> run() {
        </span><span>$this</span>-><span>getEcho ();
    }
}
</span><span>$computer</span> = <span>new</span><span> Computer ();
</span><span>$computer</span>->run ();

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

Constant (constant)
Constants can be defined in a class to represent values ​​that will not change. For any objects instantiated from this class, constant values ​​remain unchanged throughout the lifetime of those objects.

<span>class</span><span> Computer {
    </span><span>const</span> <span>PI</span> = 3.1415926<span>;
}
</span><span>echo</span> Computer::<span>PI</span>;

Static class membersSometimes it may be necessary to create fields and methods that are shared by all instances of a class. These fields and methods are relevant to all instances of the class
but cannot be used by any Called by a specific object.

<span>class</span><span> Computer {
    </span><span>public</span> <span>static</span> <span>$_count</span> = 0<span>;
}
</span><span>echo</span> Computer::<span>$_count</span>;
Generally speaking, fields must be made private. So you may need to do this:

<span>class</span><span> Computer {
    </span><span>private</span> <span>static</span> <span>$_count</span> = 0<span>;
    </span><span>public</span> <span>static</span> <span>function</span><span> setRun() {
        self</span>::<span>$_count</span> ++<span>;
    }
    </span><span>public</span> <span>static</span> <span>function</span><span> getRun() {
        </span><span>return</span> self::<span>$_count</span><span>;
    }
}
Computer</span>::<span>setRun ();
</span><span>echo</span> Computer::getRun ();

Instanceof keyword PHP5 has an instanceof keyword. Use this keyword to determine whether an object is an instance of a class, a
subclass of a class, or implements a specific interface. , and perform corresponding operations.

<span>class</span><span> Computer {
    
}
</span><span>$computer</span> = <span>new</span><span> Computer ();
</span><span>echo</span> (<span>$computer</span> instanceof Computer);

2. OOP inheritance

Inheritance is a mechanism for obtaining 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 often represented by the metaphor of parent and child. The subclass will inherit
the characteristics of the parent class. These properties consist of properties and methods. Subclasses can add new functions beyond those of the parent class, so subclasses are also called "extensions" of the parent class.

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


<span>class</span><span> Computer {
    </span><span>private</span> <span>$_name</span> = '联想120'<span>;
    </span><span>private</span> <span>function</span> __get(<span>$_key</span><span>) {
        </span><span>return</span> <span>$this</span>-><span>$_key</span><span>;
    }
    </span><span>public</span> <span>function</span><span> run() {
        </span><span>echo</span> '我是父类'<span>;
    }
}
</span><span>class</span> NoteBookComputer <span>extends</span><span> Computer {
}
</span><span>$notebookcomputer</span> = <span>new</span><span> NoteBookComputer ();
</span><span>$notebookcomputer</span>-><span>run ();
</span><span>echo</span> <span>$notebookcomputer</span>->_name;

Rewriting (overwriting) of fields and methods

Sometimes, the fields and methods of the parent class are not particularly needed, then the parent class can be modified by rewriting the subclassFields and methods.

<span>class</span><span> Computer {
    </span><span>public</span> <span>$_name</span> = '联想120'<span>;
    </span><span>protected</span> <span>function</span><span> run() {
        </span><span>echo</span> '我是父类'<span>;
    }
}

</span><span>class</span> NoteBookComputer <span>extends</span><span> Computer {
    </span><span>public</span> <span>$_name</span> = 'IBM'<span>;
    </span><span>public</span> <span>function</span><span> run() {
        </span><span>echo</span> '我是子类'<span>;
    }
}</span>

Subclasses call the fields or methods of the parent class

For safety, we generally encapsulate the methods of the parent class, so that they cannot be called externally and can only be inherited Seen by subclasses of . At this time, you need to call the parent class through the subclass operation.

<span>class</span><span> Computer {
    </span><span>protected</span> <span>$_name</span> = '联想120'<span>;
    </span><span>protected</span> <span>function</span><span> run() {
        </span><span>echo</span> '我是父类'<span>;
    }
}

</span><span>class</span> NoteBookComputer <span>extends</span><span> Computer {
    </span><span>public</span> <span>function</span><span> getName() {
        </span><span>echo</span> <span>$this</span>-><span>_name;
    }
    </span><span>public</span> <span>function</span><span> getRun() {
        </span><span>echo</span> <span>$this</span>-><span>run ();
    }
}</span>

Call the method of the parent class by overriding it

Sometimes, we need to be able to call the method content of the parent class through the overridden method. In this case, we must use the syntax: Parent class name::method() or parent::method() can be called.

<span>class</span><span> Computer {
    </span><span>protected</span> <span>function</span><span> run() {
        </span><span>echo</span> '我是父类'<span>;
    }
}

</span><span>class</span> NoteBookComputer <span>extends</span><span> Computer {
    </span><span>public</span> <span>function</span><span> run() {
        </span><span>echo</span> Computer::<span>run ();
    }
}</span>
The final keyword can prevent a class from being inherited. Sometimes you just want to be an independent class and don’t want to be inherited and used by other classes.

Then you must use this keyword. It is recommended that as long as it is a separate class, add this keyword.


<span>final</span> <span>class</span><span> Computer {
    </span><span>//</span><span>无法继承的类</span>
    <span>final</span> <span>public</span> <span>function</span><span> run() {
    } </span><span>//</span><span>无法被继承的方法</span>
<span>}

</span><span>class</span> NoteBookComputer <span>extends</span><span> Computer {
    </span><span>//</span><span>会报错</span>
}

Abstract classes and methods (abstract)

Abstract methods are very special, they are only declared in the parent class, but implemented in the child class. Only classes declared as abstract can declare abstract methods.
Rules:
1. Abstract classes cannot be instantiated and can only be inherited.

2. Abstract methods must be overridden by subclass methods.



接口(interface)
接口定义了实现某种服务的一般规范,声明了所需的函数和常量,但不指定如何实现。
之所以不给出实现的细节,是因为不同的实体可能需要用不同的方式来实现公共的方法定
义。关键是要建立必须实现的一组一般原则,只要满足了这些原则才能说实现了这个接口。

规则:
1.类全部为抽象方法(不需要声明abstract)
2.接口抽象方法必须是public
3.成员(字段)必须是常量

<span>interface</span><span> Computer {
    </span><span>const</span> NAME = '联想120'<span>;
    </span><span>public</span> <span>function</span><span> run();
}

</span><span>final</span> <span>class</span> NotebookComputer <span>implements</span><span> Computer {
    </span><span>public</span> <span>function</span><span> run() {
        </span><span>echo</span> '实现了接口的方法'<span>;
    }
}
</span><span>$notebookcomputer</span> = <span>new</span><span> NoteBookComputer ();
</span><span>$notebookcomputer</span>-><span>run ();
</span><span>echo</span> Computer::NAME;

 

子类可以实现多个接口

<span>interface</span><span> Computer {
    </span><span>const</span> NAME = '联想120'<span>;
    </span><span>public</span> <span>function</span><span> run();
}

</span><span>interface</span><span> Notebook {
    </span><span>public</span> <span>function</span><span> book();
}

</span><span>final</span> <span>class</span> NotebookComputer <span>implements</span> Computer,<span> Notebook {
    </span><span>public</span> <span>function</span><span> run() {
        </span><span>echo</span> '实现了接口的方法'<span>;
    }
    </span><span>public</span> <span>function</span><span> book() {
        </span><span>echo</span> '实现了接口的方法'<span>;
    }
}</span>

 

三.多态

多态是指OOP 能够根据使用类的上下文来重新定义或改变类的性质或行为,或者说接
口的多种不同的实现方式即为多态。把不同的子类对象都当作父类来看,可以屏蔽不同子类
对象之间的差异,写出通用的代码,做出通用的编程,以适应需求的不断变化。

<span>interface</span><span> Computer {
    </span><span>public</span> <span>function</span><span> version();
    </span><span>public</span> <span>function</span><span> work();
}

</span><span>class</span> NotebookComputer <span>implements</span><span> Computer {
    </span><span>public</span> <span>function</span><span> version() {
        
        </span><span>echo</span> '联想120'<span>;
    }
    </span><span>public</span> <span>function</span><span> work() {
        </span><span>echo</span> '笔记本正在随时携带运行!'<span>;
    }
}

</span><span>class</span> desktopComputer <span>implements</span><span> Computer {
    </span><span>public</span> <span>function</span><span> version() {
        </span><span>echo</span> 'IBM'<span>;
    }
    </span><span>public</span> <span>function</span><span> work() {
        </span><span>echo</span> '台式电脑正在工作站运行!'<span>;
    }
}

</span><span>class</span><span> Person {
    </span><span>public</span> <span>function</span> run(<span>$type</span><span>) {
        </span><span>$type</span>-><span>version ();
        </span><span>$type</span>-><span>work ();
    }
}

</span><span>$person</span> = <span>new</span><span> Person ();
</span><span>$desktopcomputer</span> = <span>new</span><span> desktopComputer ();
</span><span>$notebookcomputer</span> = <span>new</span><span> NoteBookComputer ();
</span><span>$person</span>->run ( <span>$notebookcomputer</span> );

 注:文章出自李炎恢PHP视频教程,本文仅限交流使用,不得用于商业用途,否则后果自负。

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/776507.htmlTechArticle学习要点: 1.OOP 的封装 2.OOP 的继承 3.OOP 的多态 面向对象的三个主要特性是封装、继承和多态。 一.OOP的封装 隐藏对象的字段和实现细节...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn