The three major characteristics of PHP object-oriented programming are encapsulation, inheritance and polymorphism. This article will introduce these three major features of php
Encapsulation is to add access modifiers (public, protected or private) to member properties and member methods in an object, so as to hide the internal details of the object as much as possible to achieve better control of the members. Access control
Class members defined as public can be accessed from anywhere. Class members defined as protected can be accessed by itself and its subclasses and parent classes. Class members defined as private can only be accessed by the class in which they are defined
Class attributes must be defined as one of public, protected, and private. If defined with var, it is considered public
<?<span style="color: #000000;">php </span><span style="color: #0000ff;">class</span><span style="color: #000000;"> MyClass { </span><span style="color: #0000ff;">public</span> <span style="color: #800080;">$public</span> = 'Public'<span style="color: #000000;">; </span><span style="color: #0000ff;">protected</span> <span style="color: #800080;">$protected</span> = 'Protected'<span style="color: #000000;">; </span><span style="color: #0000ff;">private</span> <span style="color: #800080;">$private</span> = 'Private'<span style="color: #000000;">; </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> printHello() { </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$this</span>-><span style="color: #0000ff;">public</span><span style="color: #000000;">; </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$this</span>-><span style="color: #0000ff;">protected</span><span style="color: #000000;">; </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$this</span>-><span style="color: #0000ff;">private</span><span style="color: #000000;">; } } </span><span style="color: #800080;">$obj</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> MyClass(); </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$obj</span>-><span style="color: #0000ff;">public</span>; <span style="color: #008000;">//</span><span style="color: #008000;"> 这行能被正常执行</span> <span style="color: #0000ff;">echo</span> <span style="color: #800080;">$obj</span>-><span style="color: #0000ff;">protected</span>; <span style="color: #008000;">//</span><span style="color: #008000;"> 这行会产生一个致命错误</span> <span style="color: #0000ff;">echo</span> <span style="color: #800080;">$obj</span>-><span style="color: #0000ff;">private</span>; <span style="color: #008000;">//</span><span style="color: #008000;"> 这行也会产生一个致命错误</span> <span style="color: #800080;">$obj</span>->printHello(); <span style="color: #008000;">//</span><span style="color: #008000;"> 输出 Public、Protected 和 Private</span> <span style="color: #0000ff;">class</span> MyClass2 <span style="color: #0000ff;">extends</span><span style="color: #000000;"> MyClass { </span><span style="color: #008000;">//</span><span style="color: #008000;"> 可以对 public 和 protected 进行重定义,但 private 而不能</span> <span style="color: #0000ff;">protected</span> <span style="color: #800080;">$protected</span> = 'Protected2'<span style="color: #000000;">; </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> printHello() { </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$this</span>-><span style="color: #0000ff;">public</span><span style="color: #000000;">; </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$this</span>-><span style="color: #0000ff;">protected</span><span style="color: #000000;">; </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$this</span>-><span style="color: #0000ff;">private</span><span style="color: #000000;">; } } </span><span style="color: #800080;">$obj2</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> MyClass2(); </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$obj2</span>-><span style="color: #0000ff;">public</span>; <span style="color: #008000;">//</span><span style="color: #008000;"> 这行能被正常执行</span> <span style="color: #0000ff;">echo</span> <span style="color: #800080;">$obj2</span>-><span style="color: #0000ff;">private</span>; <span style="color: #008000;">//</span><span style="color: #008000;"> 未定义 private</span> <span style="color: #0000ff;">echo</span> <span style="color: #800080;">$obj2</span>-><span style="color: #0000ff;">protected</span>; <span style="color: #008000;">//</span><span style="color: #008000;"> 这行会产生一个致命错误</span> <span style="color: #800080;">$obj2</span>->printHello(); <span style="color: #008000;">//</span><span style="color: #008000;"> 输出 Public、Protected2 和 Undefined</span> ?>
Methods in a class can be defined as public, private or protected. If these keywords are not set, the method defaults to public
<?<span style="color: #000000;">php </span><span style="color: #0000ff;">class</span><span style="color: #000000;"> MyClass { </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> __construct() { } </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> MyPublic() { } </span><span style="color: #0000ff;">protected</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> MyProtected() { } </span><span style="color: #0000ff;">private</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> MyPrivate() { } </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> Foo() { </span><span style="color: #800080;">$this</span>-><span style="color: #000000;">MyPublic(); </span><span style="color: #800080;">$this</span>-><span style="color: #000000;">MyProtected(); </span><span style="color: #800080;">$this</span>-><span style="color: #000000;">MyPrivate(); } } </span><span style="color: #800080;">$myclass</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> MyClass; </span><span style="color: #800080;">$myclass</span>->MyPublic(); <span style="color: #008000;">//</span><span style="color: #008000;"> 这行能被正常执行</span> <span style="color: #800080;">$myclass</span>->MyProtected(); <span style="color: #008000;">//</span><span style="color: #008000;"> 这行会产生一个致命错误</span> <span style="color: #800080;">$myclass</span>->MyPrivate(); <span style="color: #008000;">//</span><span style="color: #008000;"> 这行会产生一个致命错误</span> <span style="color: #800080;">$myclass</span>->Foo(); <span style="color: #008000;">//</span><span style="color: #008000;"> 公有,受保护,私有都可以执行</span> <span style="color: #0000ff;">class</span> MyClass2 <span style="color: #0000ff;">extends</span><span style="color: #000000;"> MyClass { </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> Foo2() { </span><span style="color: #800080;">$this</span>-><span style="color: #000000;">MyPublic(); </span><span style="color: #800080;">$this</span>-><span style="color: #000000;">MyProtected(); </span><span style="color: #800080;">$this</span>->MyPrivate(); <span style="color: #008000;">//</span><span style="color: #008000;"> 这行会产生一个致命错误</span> <span style="color: #000000;"> } } </span><span style="color: #800080;">$myclass2</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> MyClass2; </span><span style="color: #800080;">$myclass2</span>->MyPublic(); <span style="color: #008000;">//</span><span style="color: #008000;"> 这行能被正常执行</span> <span style="color: #800080;">$myclass2</span>->Foo2(); <span style="color: #008000;">//</span><span style="color: #008000;"> 公有的和受保护的都可执行,但私有的不行</span> ?>
Inheritance is a well-known programming feature, and PHP’s object model also uses inheritance. Inheritance will affect the relationship between classes and objects and objects
When extending a class, the subclass will inherit all the public and protected methods of the parent class. Unless the subclass overrides the method of the parent class, the inherited method will retain its original function
Inheritance is very useful for functional design and abstraction, and adding new functions to similar objects eliminates the need to rewrite these common functions
Class inheritance
A class can inherit the methods and properties of another class using the extends keyword in the declaration. PHP does not support multiple inheritance, a class can only inherit one base class
Inherited methods and properties can be overridden by redeclaring them with the same name. But if the parent class uses final when defining a method, the method cannot be overridden
When overriding a method, the parameters must remain consistent otherwise PHP will issue an E_STRICT level error message. The exception is the constructor, which can use different parameters when overridden
<?<span style="color: #000000;">php </span><span style="color: #0000ff;">class</span><span style="color: #000000;"> foo { </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> printItem(<span style="color: #800080;">$string</span><span style="color: #000000;">) { </span><span style="color: #0000ff;">echo</span> 'Foo: ' . <span style="color: #800080;">$string</span> . <span style="color: #ff00ff;">PHP_EOL</span><span style="color: #000000;">; } </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> printPHP() { </span><span style="color: #0000ff;">echo</span> 'PHP is great.' . <span style="color: #ff00ff;">PHP_EOL</span><span style="color: #000000;">; } } </span><span style="color: #0000ff;">class</span> bar <span style="color: #0000ff;">extends</span><span style="color: #000000;"> foo { </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> printItem(<span style="color: #800080;">$string</span><span style="color: #000000;">) { </span><span style="color: #0000ff;">echo</span> 'Bar: ' . <span style="color: #800080;">$string</span> . <span style="color: #ff00ff;">PHP_EOL</span><span style="color: #000000;">; } } </span><span style="color: #800080;">$foo</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> foo(); </span><span style="color: #800080;">$bar</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> bar(); </span><span style="color: #800080;">$foo</span>->printItem('baz'); <span style="color: #008000;">//</span><span style="color: #008000;"> Output: 'Foo: baz'</span> <span style="color: #800080;">$foo</span>->printPHP(); <span style="color: #008000;">//</span><span style="color: #008000;"> Output: 'PHP is great' </span> <span style="color: #800080;">$bar</span>->printItem('baz'); <span style="color: #008000;">//</span><span style="color: #008000;"> Output: 'Bar: baz'</span> <span style="color: #800080;">$bar</span>->printPHP(); <span style="color: #008000;">//</span><span style="color: #008000;"> Output: 'PHP is great'</span> ?>
In the subclass, use parent to access the overridden properties and methods in the parent class
<?<span style="color: #000000;">php </span><span style="color: #0000ff;">class</span><span style="color: #000000;"> Person { </span><span style="color: #0000ff;">protected</span> <span style="color: #800080;">$name</span><span style="color: #000000;">; </span><span style="color: #0000ff;">protected</span> <span style="color: #800080;">$sex</span><span style="color: #000000;">; </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> __construct(<span style="color: #800080;">$name</span>=“”, <span style="color: #800080;">$sex</span>=<span style="color: #000000;">“男”) { } </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> say(){} } </span><span style="color: #0000ff;">class</span> Student <span style="color: #0000ff;">extends</span><span style="color: #000000;"> Person { </span><span style="color: #0000ff;">private</span> <span style="color: #800080;">$school</span><span style="color: #000000;">; </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> __construct(<span style="color: #800080;">$name</span>="", <span style="color: #800080;">$sex</span>="男", <span style="color: #800080;">$school</span>=""<span style="color: #000000;">) { parent</span>::__construct(<span style="color: #800080;">$name</span>,<span style="color: #800080;">$sex</span><span style="color: #000000;">); </span><span style="color: #800080;">$this</span>->school = <span style="color: #800080;">$school</span><span style="color: #000000;">; } </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> say( ) { parent</span>::<span style="color: #000000;">say(); </span><span style="color: #0000ff;">echo</span> "在".<span style="color: #800080;">$this</span>->school."学校上学<br>"<span style="color: #000000;">; } } </span><span style="color: #800080;">$student</span> = <span style="color: #0000ff;">new</span> Student("张三","男",20, "edu"<span style="color: #000000;">); </span><span style="color: #800080;">$student</span>->say();
Abstract
In object-oriented languages, a class can have one or more subclasses, and each class has at least one public method as an interface for external code to access it. Abstract methods are introduced to facilitate inheritance
When there is a method in a class, it has no method body, that is, there are no curly braces, and it ends directly with a semicolon. We call this method an abstract method, and it must be defined using the keyword abstract
<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">abstract</span> <span style="color: #0000ff;">function</span> fun();
The class containing this method must be an abstract class and must be declared using the keyword abstract
Classes defined as abstract cannot be instantiated. Any class must be declared abstract if at least one method in it is declared abstract. A method defined as abstract only declares its calling method (parameters) and cannot define its specific function implementation
The role of an abstract method is to stipulate that subclasses must have the implementation of this method. The function is handed over to the subclass, and only the structure is written, without specific implementation. The implementation is left to the specific subclass to implement according to its own function; the abstract class The function is to require the structure of the subclass, so the abstract class is a specification
When inheriting an abstract class, the subclass must define all abstract methods in the parent class; in addition, the access control of these methods must be the same (or more relaxed) as in the parent class. For example, if an abstract method is declared as protected, then the method implemented in the subclass should be declared as protected or public, and cannot be defined as private. In addition, the method calling methods must match, that is, the type and number of required parameters must be consistent. For example, if a subclass defines an optional parameter, but it is not included in the declaration of an abstract method of the parent class, there is no conflict between the two declarations
<?<span style="color: #000000;">php </span><span style="color: #0000ff;">abstract</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> Person { </span><span style="color: #0000ff;">public</span> <span style="color: #800080;">$name</span><span style="color: #000000;">; </span><span style="color: #0000ff;">public</span> <span style="color: #800080;">$age</span><span style="color: #000000;">; </span><span style="color: #0000ff;">abstract</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> say(); </span><span style="color: #0000ff;">abstract</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> eat(); </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> run() { </span><span style="color: #0000ff;">echo</span> "11111111111111<br>"<span style="color: #000000;">; } </span><span style="color: #0000ff;">function</span> <span style="color: #008080;">sleep</span><span style="color: #000000;">() { </span><span style="color: #0000ff;">echo</span> "2222222222222222<br>"<span style="color: #000000;">; } } </span><span style="color: #0000ff;">class</span> StudentCn <span style="color: #0000ff;">extends</span><span style="color: #000000;"> Person { </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> say() { </span><span style="color: #0000ff;">echo</span> "中文<br>"<span style="color: #000000;">; } </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> eat() { </span><span style="color: #0000ff;">echo</span> "筷子"<span style="color: #000000;">; } } </span><span style="color: #0000ff;">class</span> StudentEn <span style="color: #0000ff;">extends</span><span style="color: #000000;"> Person { </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> say() { </span><span style="color: #0000ff;">echo</span> "english<br>"<span style="color: #000000;">; } </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> eat() { </span><span style="color: #0000ff;">echo</span> "刀叉"<span style="color: #000000;">; } } </span><span style="color: #800080;">$s1</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> StudentEn(); </span><span style="color: #800080;">$s1</span> -> say();<span style="color: #008000;">//</span><span style="color: #008000;">english</span> <span style="color: #800080;">$s1</span> -> eat();<span style="color: #008000;">//</span><span style="color: #008000;">刀叉</span> ?>
接口
PHP与大多数面向对象编程语言一样,不支持多重继承,也就是说每个类只能继承一个父类。为了解决这个这个问题,PHP引入了接口,接口的思想是指定了一个实现了该接口的类必须实现的一系列函数
使用接口(interface),可以指定某个类必须实现哪些方法,但不需要定义这些方法的具体内容。接口是通过interface关键字来定义的,就像定义一个标准的类一样,但其中定义所有的方法都是空的
接口中定义的所有方法都必须是公有,这是接口的特性。要实现一个接口,使用 implements 操作符。类中必须实现接口中定义的所有方法,否则会报一个致命错误。类可以实现多个接口,用逗号来分隔多个接口的名称。接口中也可以定义常量。接口常量和类常量的使用完全相同,但是不能被子类或子接口所覆盖
<span style="color: #008000;">//</span><span style="color: #008000;">实现一个接口</span> <?<span style="color: #000000;">php </span><span style="color: #0000ff;">interface</span><span style="color: #000000;"> iTemplate { </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> setVariable(<span style="color: #800080;">$name</span>, <span style="color: #800080;">$var</span><span style="color: #000000;">); </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> getHtml(<span style="color: #800080;">$template</span><span style="color: #000000;">); } </span><span style="color: #0000ff;">class</span> Template <span style="color: #0000ff;">implements</span><span style="color: #000000;"> iTemplate { </span><span style="color: #0000ff;">private</span> <span style="color: #800080;">$vars</span> = <span style="color: #0000ff;">array</span><span style="color: #000000;">(); </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> setVariable(<span style="color: #800080;">$name</span>, <span style="color: #800080;">$var</span><span style="color: #000000;">) { </span><span style="color: #800080;">$this</span>->vars[<span style="color: #800080;">$name</span>] = <span style="color: #800080;">$var</span><span style="color: #000000;">; } </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> getHtml(<span style="color: #800080;">$template</span><span style="color: #000000;">) { </span><span style="color: #0000ff;">foreach</span>(<span style="color: #800080;">$this</span>->vars <span style="color: #0000ff;">as</span> <span style="color: #800080;">$name</span> => <span style="color: #800080;">$value</span><span style="color: #000000;">) { </span><span style="color: #800080;">$template</span> = <span style="color: #008080;">str_replace</span>('{' . <span style="color: #800080;">$name</span> . '}', <span style="color: #800080;">$value</span>, <span style="color: #800080;">$template</span><span style="color: #000000;">); } </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$template</span><span style="color: #000000;">; } } </span>?>
<span style="color: #008000;">//</span><span style="color: #008000;">常量不能被覆盖</span> <?<span style="color: #000000;">php </span><span style="color: #0000ff;">interface</span><span style="color: #000000;"> a { </span><span style="color: #0000ff;">const</span> b = 'Interface constant'<span style="color: #000000;">; } </span><span style="color: #0000ff;">echo</span> a::<span style="color: #000000;">b; </span><span style="color: #008000;">//</span><span style="color: #008000;"> 错误写法,因为常量不能被覆盖。接口常量的概念和类常量是一样的。</span> <span style="color: #0000ff;">class</span> b <span style="color: #0000ff;">implements</span><span style="color: #000000;"> a { </span><span style="color: #0000ff;">const</span> b = 'Class constant'<span style="color: #000000;">; } </span>?>
<span style="color: #008000;">//</span><span style="color: #008000;">继承多个接口</span> <?<span style="color: #000000;">php </span><span style="color: #0000ff;">interface</span><span style="color: #000000;"> a { </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> foo(); } </span><span style="color: #0000ff;">interface</span><span style="color: #000000;"> b { </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> bar(); } </span><span style="color: #0000ff;">interface</span> c <span style="color: #0000ff;">extends</span> a,<span style="color: #000000;"> b { </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> baz(); } </span>?>
对象的多态性是指在父类中定义的属性或行为被子类继承之后,可以具有不同的数据类型或表现出不同的行为。这使得同一个属性或行为在父类及其各个子类中具有不同的语义。例如:"几何图形"的"绘图"方法,"椭圆"和"多边形"都是"几何图"的子类,其"绘图"方法功能不同
单态
说到多态,首先要提到单态设计模式,单态模式的主要作用是保证在面向对象编程设计中,一个类只能有一个实例对象存在
<?<span style="color: #000000;">php </span><span style="color: #0000ff;">class</span><span style="color: #000000;"> DB { </span><span style="color: #0000ff;">private</span> <span style="color: #0000ff;">static</span> <span style="color: #800080;">$obj</span> = <span style="color: #0000ff;">null</span><span style="color: #000000;">; </span><span style="color: #0000ff;">private</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> __construct() { </span><span style="color: #0000ff;">echo</span> "连接数据库成功<br>"<span style="color: #000000;">; } </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> getInstance() { </span><span style="color: #0000ff;">if</span>(<span style="color: #008080;">is_null</span>(self::<span style="color: #800080;">$obj</span><span style="color: #000000;">)) self</span>::<span style="color: #800080;">$obj</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> self(); </span><span style="color: #0000ff;">return</span> self::<span style="color: #800080;">$obj</span><span style="color: #000000;">; } </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> query(<span style="color: #800080;">$sql</span><span style="color: #000000;">) { </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$sql</span><span style="color: #000000;">; } } </span><span style="color: #800080;">$db</span> = DB::<span style="color: #000000;">getInstance(); </span><span style="color: #800080;">$db</span> -> query("select * from user"<span style="color: #000000;">); </span>?>
多态展现了动态绑定的功能,也称为“同名异式”,多态可以让软件在开发和维护时,达到充分的延伸性
在php中,多态性就是指方法的重写,一个子类可中可以重新修改父类中的某些方法,使其具有自己的特征。重写要求子类的方法和父类的方法名称相同,这可以通过声明抽象类或是接口来规范
<?<span style="color: #000000;">php </span><span style="color: #0000ff;">interface</span><span style="color: #000000;"> USB { </span><span style="color: #0000ff;">const</span> WIDTH = 12<span style="color: #000000;">; </span><span style="color: #0000ff;">const</span> HEIGHT = 3<span style="color: #000000;">; </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> load(); </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> run(); </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> stop(); } </span><span style="color: #0000ff;">class</span><span style="color: #000000;"> Cumputer { </span><span style="color: #0000ff;">function</span> useUSB(USB <span style="color: #800080;">$usb</span><span style="color: #000000;">) { </span><span style="color: #800080;">$usb</span> -><span style="color: #000000;"> load(); </span><span style="color: #800080;">$usb</span> -><span style="color: #000000;"> run(); </span><span style="color: #800080;">$usb</span> -><span style="color: #000000;"> stop(); } } </span><span style="color: #0000ff;">class</span> Mouse <span style="color: #0000ff;">implements</span><span style="color: #000000;"> USB{ </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> load() { </span><span style="color: #0000ff;">echo</span> "加载鼠标成功!<br>"<span style="color: #000000;">; } </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> run() { </span><span style="color: #0000ff;">echo</span> "运行鼠标功能!<br>"<span style="color: #000000;">; } </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> stop() { </span><span style="color: #0000ff;">echo</span> "鼠标工作结束!<br>"<span style="color: #000000;">; } } </span><span style="color: #0000ff;">class</span> KeyPress <span style="color: #0000ff;">implements</span><span style="color: #000000;"> USB { </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> load() { </span><span style="color: #0000ff;">echo</span> "加载键盘成功!<br>"<span style="color: #000000;">; } </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> run() { </span><span style="color: #0000ff;">echo</span> "运行键盘成功!<br>"<span style="color: #000000;">; } </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> stop() { </span><span style="color: #0000ff;">echo</span> "停止键盘使用!<br>"<span style="color: #000000;">; } } </span><span style="color: #0000ff;">class</span><span style="color: #000000;"> Worker { </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> work() { </span><span style="color: #800080;">$c</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> Cumputer(); </span><span style="color: #800080;">$m</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> Mouse; </span><span style="color: #800080;">$k</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> KeyPress; </span><span style="color: #800080;">$c</span>->useUSB(<span style="color: #800080;">$k</span><span style="color: #000000;">); </span><span style="color: #800080;">$c</span>->useUSB(<span style="color: #800080;">$m</span><span style="color: #000000;">); } } </span><span style="color: #800080;">$w</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> Worker; </span><span style="color: #800080;">$w</span> -><span style="color: #000000;"> work(); </span>?>