每天一记之php原型模式
每日一记之php原型模式
原型模式是指通过现有的实例通过拷贝得到新的实例。
在程序的设计中,有的时候我们去实例化某个对象需要做太多的初始化工作,非常耗时的时候,我们可以考虑采用原型模式来得到新的实例。
其实在php中我们很容易通过clone关键字去实现对象的复制。另外通过魔术方法__clone()指定在clone的时候需要进行的操作。这个其实就是原型模式的实现方式了。当然,有时候为了让代码看起来比较优雅,比较完善。我们可以自己去写相关的实现方式,当然也需要用到clone关键字。
<?phpinterface Cloneable{ public function copy();}class Task implements Cloneable{ public $name; public $startTime; public function __construct($name){ //这里实例化Task需要做很多工作 $this->name = $name; $this->startTime = time(); } public function copy(){ return clone $this; }}
在这里我们可以创建一个task
$task1 = new Task("Task1");
现在我们已经有了一个Task的实例了,我们要得到一个新的task实例就可以通过clone的方法
$task2 = $task1->copy();
可是现在这样我们打印$task1和$task2的startTime,两者是一样的,而我们又希望clone出来的对象时间应该是当前时间,怎么做呢?所以我们就得去写__clone方法,该方法在一个对象尝试进行clone的时候会自动调用。改良后的代码如下
class Task implements Cloneable{ public $name; public $startTime; public function __construct($name){ //这里实例化Task需要做很多工作 $this->name = $name; $this->startTime = time(); } public function __clone(){ //在克隆的时候把startTime设为当前时间 $this->startTime = time(); //克隆的时候需要执行的操作 } public function copy(){ return clone $this; }}
这样克隆出来的对象的startTime就更正为当前时间了。
当然了,这只是一个简单的原型模式,在实际的克隆中,又分为浅克隆和深克隆。
浅克隆: 即对象在调用clone方法时只克隆基本的数据类型,而如果对象中包含其他对象的引用时,则copy其他对象的引用
深克隆:即除了克隆基本的数据类型外,引用的类型的数据也一并克隆。
举个例子,现在我的task类里面有一个其他对象的引用,比如Parent。相关代码如下:
class TaskParent{ public $name; public function __construct($name){ $this->name = $name; }}class Task implements Cloneable{ public $name; public $startTime; public $parent; public function __construct($name){ //这里实例化Task需要做很多工作 $this->name = $name; $this->startTime = time(); //这里直接new一个parent $this->parent = new TaskParent("do some work"); } public function __clone(){ //在克隆的时候把startTime设为当前时间 $this->startTime = time(); //克隆的时候需要执行的操作 } public function copy(){ return clone $this; }}
在上面的代码里,我们新定义了一个TaskParent的类,然后Task持有该对象的一个引用,客户端代码:
$task1 = new Task("Task1");echo $task1->parent->name."\r\n";//clone一个对象$task2 = $task1->copy();echo $task2->parent->name."\r\n";//将task1的parent的name设为另外的值$task1->parent->name = "do another work";//打印task2的parent的值echo $task2->parent->name;
class Task implements Cloneable{ public $name; public $startTime; public $parent; public function __construct($name){ //这里实例化Task需要做很多工作 $this->name = $name; $this->startTime = time(); //这里直接new一个parent $this->parent = new TaskParent("do some work"); } public function __clone(){ //在克隆的时候把startTime设为当前时间 $this->startTime = time(); //克隆parent $this->parent = clone $this->parent; //克隆的时候需要执行的操作 } public function copy(){ return clone $this; }}
我们在__clone方法里面对parent也进行了一次克隆,所以现在打印刚才的代码就没有问题了。这就是深克隆。
在真实的编码环境中,可能一个对象持有很多其他的对象的引用,而其他对象对象又持有很多的引用。由于引用的不确定性,我们一开始的时候就应该注意,到底哪些对象需要深克隆,那些对象不需要。
如果想实现快速的深克隆,网上一哥们提供了一个简单的方法代码如下:
class Task implements Cloneable{ public $name; public $startTime; public $parent; public function __construct($name){ //这里实例化Task需要做很多工作 $this->name = $name; $this->startTime = time(); //这里直接new一个parent $this->parent = new TaskParent("do some work"); } public function __clone(){ //在克隆的时候把startTime设为当前时间 $this->startTime = time(); //克隆parent $this->parent = clone $this->parent; //克隆的时候需要执行的操作 } public function copy(){ //return clone $this; //这里不使用clone关键字,而是使用序列化和反序列化来进行 return unserialize(serialize($this)); }}
上述代码中通过serialize和unserialize来得到新的实例,经测试,完全是深克隆。关于serialize和unserialize的效率以及具体讲解将在以后补充上

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



Recently, many Win11 users have reported that when shutting down, they are prompted that the taskhostwindow task host is executing the shutdown task. So what is going on? Users can enter the Desktop folder under the local registry editor, and then select AutoEndTasks in the right window to set it. Let this site carefully introduce to users the solution to this problem when shutting down. Windows 11 shutdown prompts that the taskhostwindow task host is executing the shutdown task. Solution 1. Use the key combination win key + r key, enter "regedit" and press Enter, as shown in the figure below. 2. Search for [HKEY

Every year before Apple releases a new major version of iOS and macOS, users can download the beta version several months in advance and experience it first. Since the software is used by both the public and developers, Apple has launched developer and public versions, which are public beta versions of the developer beta version, for both. What is the difference between the developer version and the public version of iOS? Literally speaking, the developer version is a developer test version, and the public version is a public test version. The developer version and the public version target different audiences. The developer version is used by Apple for testing by developers. You need an Apple developer account to download and upgrade it.

php提交表单通过后,弹出的对话框怎样在当前页弹出php提交表单通过后,弹出的对话框怎样在当前页弹出而不是在空白页弹出?想实现这样的效果:而不是空白页弹出:------解决方案--------------------如果你的验证用PHP在后端,那么就用Ajax;仅供参考:HTML code

Inobject-orientedprogramming,inheritanceallowsustocreatenewclassesthatinheritthepropertiesandmethodsofanexistingclass.Thispowerfulconceptenablescodereuse,modularity,andextensibilityinourprograms.Beforedivingintoaccessingparentclassattributes,let'shav

The Java public access modifier allows functions to be accessed from anywhere and is used to declare public APIs and define tools and utilities that are shared across packages or classes. The specific usage is as follows: Syntax: public return value type function name (parameter list) {...} Scenario: functions that need to be accessed from anywhere, methods in public APIs, shared tools or utilities

Task is an object used to represent asynchronous operations in C#. It is located in the System.Threading.Tasks namespace. Task provides a high-level API for handling concurrent, asynchronous operations, making it easier to write asynchronous code in .NET applications.

PHP cannot get the name because when the name and id values of the form element are different, the browser cannot recognize it. The solution: 1. Check whether some form elements and frame elements use name; 2. Check only Elements that can be assigned ID but not name; 3. For multi-select box checkbox, you can use "join(',', $__POST['name'])" to form data.

What is the use of name in Vue3? 1. Name needs to be defined when making recursive components. 2. The component can be cached with keep-aliveincludeexclude. 3. When Vue reports an error or is debugging, you can see the name of the component. Vue3 defines name1. It is automatically generated as long as the setup syntax sugar mode single file component is turned on in the script. The corresponding name option will be automatically generated based on the file name. For example, Tree.vue, then its name will be automatically generated by Tree. This has a drawback. If you want to modify the name, you need to modify the component name. If there is a place to import the component, you need to modify it together. 2. Open a script to define name
