PHP 面向对对象基础(接口,类),php面向
PHP 面向对对象基础(接口,类),php面向
介绍PHP面向对象的基础知识
1. 接口的定义interface ,类定义class,类支持abstract和final修饰符,abstract修饰为抽象类,抽象类
不支持直接实例化,final修饰的类/方法不能被继承/方法重写.
2. 接口的实现通过implements,类继承extends
<span>interface</span><span> IShape{ </span><span>function</span><span> draw_core(); }; </span><span>class</span> PathShape <span>implements</span><span> IShape{ </span><span>public</span> <span>function</span><span> draw_core(){} } </span><span>class</span> Rectangle <span>extends</span><span> PathShape{ </span><span>public</span> <span>function</span><span> draw_core(){ </span><span>//</span><span>overide draw_core</span> <span> } }</span>
3.静态变量和常量(static ,const )
a.常量声明变量名前面不需要加美元修饰符$,静态变量需要
b.两者都通过类访问,静态变量方法时候需要在变量名前加$美元修饰符好
<span>class</span><span> MyClass{ </span><span>const</span><span> M_CONST_VALUE; </span><span>static</span> <span>$M_STATIC_VALUE</span><span>; } MyClass</span>::<span>M_CONST_VALUE ; MyClass</span>::<span>$M_STATIC_VALUE</span>;
c.常量声明时候不支持访问权限修饰符,不能在const前加public,常量默认就是public。
<span>const</span> M_CONST ; <span>//</span><span>OK</span> <span>public</span> <span>const</span> M_CONST ; <span>//</span><span> throw exception</span>
4.类内部访问非静态/常量变量和方法通过$this,访问父类通过parent,在类内部访问静态变量和方法可以通过
self,self本质是指向该类也可以通过static访问
parent::method(); <span>//</span><span>父类方法</span> <span>$this</span>->method() ; <span>//</span><span>方法实例方法</span> self::<span>$static_value</span> ;<span>//</span><span>访问静态变量</span> <span>static</span>::<span>$static_value</span>;<span>//</span><span>同上</span>
5.static和self的区别在于self指的是解析上下文,也是是作用与当前类,static指的是被调用
的类而不是包含类,典型的例子就是单例
<span>abstract</span> <span>class</span><span> ParentClass{ </span><span>public</span> <span>static</span> <span>function</span><span> createInstance(){ </span><span>return</span> <span>new</span> <span>static</span><span>(); </span><span>//</span><span>这里不能使用self,因为self本意其实指向parentclass的 //如果你使用了self,那么将抛出异常,提示抽象类无法实例化 //而static并不直接指向parentclass而是作用与包含类 //</span> <span> } } </span><span>class</span> ChildClass <span>extends</span><span> ParentClass{ </span><span>// </span> }
7.类中使用拦截器,PHP拦截器有__get,__set,__inset,__unset,__call,这里只关注geth和set拦截器
__get(<span>$property</span><span>) 当访问未定义的属性时候该方法被调用 __set(</span><span>$property</span>,<span>$value</span><span>)当给未定义的属性赋值时被调用 </span><span>class</span><span> MyClass{ </span><span>public</span> <span>function</span> __get(<span>$property</span><span>){ </span><span>echo</span> "Access __get"<span>; </span><span>if</span>(property_exists(<span>$this</span>,<span>$property</span><span>)){ </span><span>return</span> <span>$this</span>-><span>$property</span><span>; }</span><span>else</span><span>{ </span><span>return</span> "unknown"<span>; } } </span><span>public</span> <span>function</span> __set(<span>$property</span>,<span>$value</span><span>){ </span><span>if</span>(!property_exists(<span>$this</span>,<span>$property</span><span>)){ </span><span>$this</span>->Name = <span>$value</span>; <span>//</span><span>变量不存在就直接给$Name赋值</span> <span> } } </span><span>public</span> <span>$Name</span> = "visonme"<span>; }; </span><span>//</span><span>访问</span> <span>$obj</span> = <span>new</span><span> MyClass(); </span><span>$obj</span>->Name ; <span>//</span><span>直接访问变量$Name</span> <span>$obj</span>->Password;<span>//</span><span>Password未定义,先访问__get最后输出unknown //-for __set</span> <span>$obj</span>->password = 'fz-visonme';<span>//</span><span>password不存在,那么将走__setz最后给$Name赋值</span> <span>echo</span> <span>$obj</span>->Name ; <span>//</span><span> output: fz-visonme</span>
8.类构造函数和析构函数:__construct, __destruct ,构造函数实例化对象时候调用,多用于成员变量初始化工作,析构在类销毁时候调用,多用于收尾工作
<span>class</span><span> MyClass{ </span><span>function</span><span> __construct(){} </span><span>function</span><span> __destruct(){} }</span>
9.对象的复制通过clone,clone关键字使用“值复制"方式来产生一个新的对象,对于对象复制本身还是走引用复制的。
a.简单类型赋值
<span>class</span><span> MyClass{ </span><span>public</span> <span>$ID</span><span>; }; </span><span>$a</span> = <span>new</span><span> MyClass; </span><span>$a</span>->ID = 199<span>; </span><span>$b</span> = <span>clone</span> <span>$a</span><span>; </span><span>echo</span> <span>$b</span>->ID; <span>//</span><span> output: 199</span>
b.包含对象的复制
<span>class</span><span> Account{ </span><span>public</span> <span>$RMB</span><span>; }; </span><span>class</span><span> MyClass{ </span><span>public</span> <span>$ID</span><span>; </span><span>public</span> <span>$AccountObj</span><span>; </span><span>public</span> <span>function</span> __construct(<span>$c</span><span>){ </span><span>$this</span>->AccountObj = <span>$c</span><span>; } }; </span><span>$a</span> = <span>new</span> MyClass(<span>new</span><span> Account()); </span><span>$a</span>->AccountObj->RMB= 199<span>; </span><span>$b</span> = <span>clone</span> <span>$a</span><span>; </span><span>echo</span> <span>$b</span>->AccountObj->RMB; <span>//</span><span>output: 199</span> <span>$a</span>->AccountObj->RMB = 100<span>; </span><span>echo</span> <span>$b</span>->AccountObj->RMB; <span>//</span><span>output: 100</span> <span> 在clone后,</span><span>$a的AccountObj改变时候</span>,同时会影响到<span>$b</span>
这种结果显然不是我们所期望的,我们所期望的是ab是两个不存在任何关联的独立对象.
为了解决这个问题我么可以在类内部实现__clone,当我们在外面调用clone时候其内部会调用类的__clonef方法,所以我们可以通过重写__clone来达到对clone的控制.例如针对b例子的改造
<span>class</span><span> MyClass{ </span><span>public</span> <span>$ID</span><span>; </span><span>public</span> <span>$AccountObj</span><span>; </span><span>public</span> <span>function</span> __construct(<span>$c</span><span>){ </span><span>$this</span>->AccountObj = <span>$c</span><span>; } </span><span>//</span><span>__clone实现clone的控制 //这里内部同时对Account实现一次clone,这里就可以避免b例子中出现的问题</span> <span>public</span> <span>function</span><span> __clone(){ </span><span>$this</span>->ID = 0 ; <span>//</span><span>将ID置为0,如果你需要的话</span> <span>$this</span>->AccountObj = <span>clone</span> <span>$this</span>-><span>AccountObj; } };</span>
关于__clone方法我们需要知道,该方法是在被clone后的对象上调用,而不是在原始的对象上面运行的,例如上b例子中
$b = clone $a ; //执行的过程: 基本复制对象$a ---> $b执行__clone()

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Golang has no abstract classes. Golang is not an object-oriented (OOP) language. It has no concepts of classes, inheritance, and abstract classes. However, there are structures (structs) and interfaces (interfaces) in golang, which can be indirectly implemented through the combination of struct and interface. Abstract classes in object languages.

Java allows inner classes to be defined within interfaces and abstract classes, providing flexibility for code reuse and modularization. Inner classes in interfaces can implement specific functions, while inner classes in abstract classes can define general functions, and subclasses provide concrete implementations.

Interface Interface defines abstract methods and constants in Java. The methods in the interface are not implemented, but are provided by the class that implements the interface. The interface defines a contract that requires the implementation class to provide specified method implementations. Declare the interface: publicinterfaceExampleInterface{voiddoSomething();intgetSomething();} Abstract class An abstract class is a class that cannot be instantiated. It contains a mixture of abstract and non-abstract methods. Similar to interfaces, abstract methods in abstract classes are implemented by subclasses. However, abstract classes can also contain concrete methods, which provide default implementations. Declare abstract class: publicabstractcl

Interface: An implementationless contract interface defines a set of method signatures in Java but does not provide any concrete implementation. It acts as a contract that forces classes that implement the interface to implement its specified methods. The methods in the interface are abstract methods and have no method body. Code example: publicinterfaceAnimal{voideat();voidsleep();} Abstract class: Partially implemented blueprint An abstract class is a parent class that provides a partial implementation that can be inherited by its subclasses. Unlike interfaces, abstract classes can contain concrete implementations and abstract methods. Abstract methods are declared with the abstract keyword and must be overridden by subclasses. Code example: publicabstractcla

Interfaces and abstract classes are used in design patterns for decoupling and extensibility. Interfaces define method signatures, abstract classes provide partial implementation, and subclasses must implement unimplemented methods. In the strategy pattern, the interface is used to define the algorithm, and the abstract class or concrete class provides the implementation, allowing dynamic switching of algorithms. In the observer pattern, interfaces are used to define observer behavior, and abstract or concrete classes are used to subscribe and publish notifications. In the adapter pattern, interfaces are used to adapt existing classes. Abstract classes or concrete classes can implement compatible interfaces, allowing interaction with original code.

Both functional interfaces and abstract classes are used for code reusability, but they are implemented in different ways: functional interfaces through reference functions, abstract classes through inheritance. Functional interfaces cannot be instantiated, but abstract classes can. Functional interfaces must implement all declared methods, while abstract classes can only implement some methods.

Tips for optimizing the performance of interfaces and abstract classes in Java: Avoid using default methods in interfaces and only use them when necessary. Minimize interface definition to include only necessary content. Implement as many abstract class methods as possible. Use the final modifier to prevent overriding by subclasses. Declare methods that should not be called as private.

Differences: 1. Abstract classes can have attributes and ordinary methods, but interfaces cannot; 2. There may not be abstract methods in abstract classes, but there must be "abstract" methods in interfaces; 3. There are differences in syntax; 4. Abstract classes Use the abstract keyword to declare before a class, and a class is declared as a class. The interface is declared with interface, but cannot be declared with class; 5. The abstract method of an abstract class must be declared with abstract, but interfaces are not required; 6. Abstract classes and interfaces implement detailed methods in different ways.
