PHP object-oriented cloning
One of the biggest disadvantages of PHP4 object-oriented is that it treats objects as another data type, which makes many common OOP methods unusable, such as design patterns. These OOP methods rely on passing objects to other class methods as references, rather than as values. Fortunately PHP solves this problem. All objects are now treated as references by default. But because all object pairs are treated as references rather than values, it is now harder to copy objects. If you try to copy an object, this will point to the address of the original object. To solve the duplication problem, PHP provides a method to clone the display object.
Examples are as follows:
First introduce the use of clone keyword to clone objects:
<!--?php class TestClone { public $name; public $num; function setName($na) { $this--->name = $na; } function getName() { return $this->name; } function setNum($nu) { $this->num = $nu; } function getNum() { return $this->num; } } $test = new TestClone(); $test->setName("tianxin"); $test->setNum(123456); echo $test->getName(); echo $test->getNum()." "; $test2 = clone $test; $test2->setName("liwei"); echo $test->getName(); echo $test->getNum()." "; echo $test2->getName(); echo $test2->getNum(); ?>Running results:
tian123456 tian123456 xia123456
From the running results, we can see that if test2 does not modify the name. Although the two objects test and test2 are different objects, they have the same attributes, and changing the attributes of the test2 object will not affect the attributes of the test object. Therefore, it can be concluded that cloning is passing by value, not a simple reference.
PHP5 defines a special method name "__clone()" method, which is automatically called when an object is cloned. Using the "__clone()" method will create an object with the same properties and methods as the original object. If If you want to change the content of the original object after cloning, you need to rewrite the original attributes and methods in __clone(). The "__clone()" method can have no parameters, and it automatically contains two pointers, $this and $that, $this points to the copy, while $that points to the original;
<!--?php class TestClone { public $name; public $num; function setName($na) { $this--->name = $na; } function getName() { return $this->name; } function setNum($nu) { $this->num = $nu; } function getNum() { return $this->num; } function __clone() { $this->name = "huang"; } } $test = new TestClone(); $test->setName("tian"); $test->setNum(123456); echo $test->getName(); echo $test->getNum()." "; $test2 = clone $test; // $test2->setName("xia"); echo $test->getName(); echo $test->getNum()." "; echo $test2->getName(); echo $test2->getNum(); ?>
Running results:
tian123456 tian123456 huang123456
<!--?php class Person { // 下面是人的成员属性 var $name; // 人的名字 var $sex; // 人的性别 var $age; // 人的年龄 // 定义一个构造方法参数为属性姓名$name、性别$sex 和年龄$age 进行赋值 // function __construct($name="", $sex="",$age="") function __construct($name, $sex, $age) { $this--->name = $name; $this->sex = $sex; $this->age = $age; } // 这个人可以说话的方法, 说出自己的属性 function say() { echo "我的名字叫:" . $this->name . " 性别:" . $this->sex . " 我的年龄是:" . $this ->age . " "; } // 对象克隆时自动调用的方法, 如果想在克隆后改变原对象的内容,需要在__clone()中重写原来的属性和方法。 function __clone() { // $this 指的复本p2, 而$that 是指向原本p1,这样就在本方法里,改变了复本的属性。 $this->name = "我是复制的张三$that->name"; // $this->age = 30; } } $p1 = new Person ( "张三", "男", 20 ); $p2 = clone $p1; $p1->say (); $p2->say (); ?>
Result after running:
我的名字叫:张三 性别:男 我的年龄是:20 我的名字叫:我是复制的张三 性别:男 我的年龄是:20

JSON(JavaScriptObjectNotation)是一种轻量级的数据交换格式,已经成为Web应用程序之间数据交换的常用格式。PHP的json_encode()函数可以将数组或对象转换为JSON字符串。本文将介绍如何使用PHP的json_encode()函数,包括语法、参数、返回值以及具体的示例。语法json_encode()函数的语法如下:st

楔子我们知道对象被创建,主要有两种方式,一种是通过Python/CAPI,另一种是通过调用类型对象。对于内置类型的实例对象而言,这两种方式都是支持的,比如列表,我们即可以通过[]创建,也可以通过list(),前者是Python/CAPI,后者是调用类型对象。但对于自定义类的实例对象而言,我们只能通过调用类型对象的方式来创建。而一个对象如果可以被调用,那么这个对象就是callable,否则就不是callable。而决定一个对象是不是callable,就取决于其对应的类型对象中是否定义了某个方法。如

使用Python的__contains__()函数定义对象的包含操作Python是一种简洁而强大的编程语言,提供了许多强大的功能来处理各种类型的数据。其中之一是通过定义__contains__()函数来实现对象的包含操作。本文将介绍如何使用__contains__()函数来定义对象的包含操作,并且给出一些示例代码。__contains__()函数是Pytho

标题:使用Python的__le__()函数定义两个对象的小于等于比较在Python中,我们可以通过使用特殊方法来定义对象之间的比较操作。其中之一就是__le__()函数,它用于定义小于等于比较。__le__()函数是Python中的一个魔法方法,并且是一种用于实现“小于等于”操作的特殊函数。当我们使用小于等于运算符(<=)比较两个对象时,Python

Javascript对象如何循环遍历?下面本篇文章给大家详细介绍5种JS对象遍历方法,并浅显对比一下这5种方法,希望对大家有所帮助!

Python中如何使用getattr()函数获取对象的属性值在Python编程中,我们经常会遇到需要获取对象属性值的情况。Python提供了一个内置函数getattr()来帮助我们实现这个目标。getattr()函数允许我们通过传递对象和属性名称作为参数来获取该对象的属性值。本文将详细介绍getattr()函数的用法,并提供实际的代码示例,以便更好地理解。g

使用Python的isinstance()函数判断对象是否属于某个类在Python中,我们经常需要判断一个对象是否属于某个特定的类。为了方便地进行类别判断,Python提供了一个内置函数isinstance()。本文将介绍isinstance()函数的用法,并提供代码示例。isinstance()函数可以判断一个对象是否属于指定的类或类的派生类。它的语法如下

PHP代码封装技巧:如何使用类和对象封装可重复使用的代码块摘要:在开发中,经常遇到需要重复使用的代码块。为了提高代码的可维护性和可重用性,我们可以使用类和对象的封装技巧来对这些代码块进行封装。本文将介绍如何使用类和对象封装可重复使用的代码块,并提供几个具体的代码示例。使用类和对象的封装优势使用类和对象的封装有以下几个优势:1.1提高代码的可维护性通过将重复


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

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version
Visual web development tools

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

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft
