Nhiều loại dữ liệu có thể được sao chép trên-viết, chẳng hạn như $ a = $ b và hai biến cho các giá trị bằng nhau. Nó khác nhau đối với các đối tượng:
It is different for objects:
$box1 = new Parcel[];
$box1->destinationCountry = 'Denmark';$box2 = $box1;
$box2->destinationCountry = 'Brazil';echo 'Parcels need to ship to:' . $box1->destinationCountry . ' and ' . $box2->destinationCountry;
//Print result
//Parcels need to ship to: Brazil and Brazil
Tình huống hiện tại là khi $ Box1 được gán cho $ Box2, giá trị của $ Box1 không được sao chép. Thay vào đó, PHP sử dụng một cách khác để point $ box2 vào cùng một đối tượng, gọi nó là reference
. Bằng cách so sánh hai đối tượng với toán tử ==, bạn có thể thấy chúng có cùng các lớp và thuộc tính không.
By comparing the two objects with the == operator, you can see if they have the same classes and attributes.
if[$box1 == $box2] echo 'equivalent';
Bạn có thể phân biệt thêm liệu chúng có tham khảo cùng một đối tượng gốc hay không và so sánh chúng theo cùng một cách: === Toán tử:
if[$box1 === $box2] echo 'exact same object!';
Toán tử so sánh === trả về true khi hai biến trỏ đến cùng một giá trị. Nếu các đối tượng giống hệt nhau nhưng được lưu trữ ở các vị trí khác nhau, nó sẽ trả về sai.
Objects are always passed by reference
. Đó là, khi chuyển một đối tượng cho một hàm, hàm sẽ hoạt động trên cùng một đối tượng. Nếu đối tượng thay đổi bên trong hàm, thay đổi sẽ được phản ánh bên ngoài hàm. Đây là một phần mở rộng của hành vi gán một đối tượng cho một biến mới. Các đối tượng luôn hoạt động theo cách mà chúng cung cấp một cho đối tượng ban đầu reference
, không phải là một đối tượng tự tạo ra copy
.
Objects always behave in such a way that they provide one to the original object reference
, not one that creates itself copy
.
$courier = new PigeonPost['Avian Delivery Ltd'];$other_courier = $courier;
$other_courier->name = 'Pigeon Post';echo $courier->name; // outputs "Pigeon Post"
Đối tượng sẽ cung cấp một tham chiếu đến chính nó, thay vì sao chép một bản sao của chính nó. Điều này có nghĩa là nếu một hàm hoạt động trên một đối tượng được truyền vào, không cần phải quay lại từ hàm. Thay đổi này được phản ánh trong bản sao gốc của đối tượng.
Nếu bạn cần sao chép một bản sao riêng của một đối tượng hiện có, bạn có thể sử dụng từ khóa ____10. này để tạo nó.
$courier = new PigeonPost['Avian Delivery Ltd'];$other_courier = clone $courier;
$other_courier->name = 'Pigeon Post';echo $courier->name; // outputs "Avian Delivery Ltd"
Khi một đối tượng được sao chép, bất kỳ đối tượng được lưu trữ trong các thuộc tính của nó sẽ là một tham chiếu thay vì một bản sao. PHP có một cách kỳ diệu, nghĩa là, nếu một đối tượng được khai báo, nó sẽ được gọi khi đối tượng được sao chép. Đây là phương thức _clone [], mà bạn có thể khai báo và sử dụng để quyết định
if[$box1 == $box2] echo 'equivalent';
1.PHP has a magical way, that is, if an object is declared, it will be called when the object is copied. This is the _clone[] method, which you can declare and use to decide
if[$box1 == $box2] echo 'equivalent';
1.Một giao diện trơn tru luôn được truyền qua tham chiếu, điều đó có nghĩa là không cần phải trả về một đối tượng từ một phương thức để quan sát các thay đổi của nó. Tuy nhiên, nếu bạn trả về $ điều này từ một phương thức, bạn có thể tạo một giao diện thông thạo trong ứng dụng cho phép bạn liên kết các phương thức với nhau. Nguyên tắc làm việc như sau: 1. Tạo đối tượng 2. Phương thức gọi đối tượng 3. Nhận đối tượng đã sửa được trả về từ phương thức 4. Chọn trở lại Bước 2
object is always passed by reference, which means that there is no need to return an object from a method to observe its changes. However, if you return $this from a method, you can create a fluent interface within the application that lets you link methods together. The working principle is as follows:
1. Create object
2. Call object method
3. Get the corrected object returned from the method
4. Select back to step 2
class Parcel
{
protected $weight;
protected $destinationCountry;public function setWeight[$weight] {
echo "weight set to: " . $weight . "\n";
$this->weight = $weight;
return $this;
}public function setCountry[$country] {
$myParcel = new Parcel;
echo "destination country is: " . $country . "\n";
$this->destinationCountry = $country;
return $this;
}
}
$myParcel->setWeight[5]->setCountry['Egypt'];
Chìa khóa ở đây là bạn có thể gọi nhiều phương thức trong một dòng mã duy nhất [bạn có thể thêm một số lần ngắt dòng để tăng khả năng đọc của mã] và gọi chúng theo bất kỳ thứ tự nào. Vì mỗi phương thức trả về đối tượng được tạo, bạn có thể gọi phương thức tiếp theo bằng cách trả về đối tượng.
Một trong những điểm chính của PHP OOP thường được đề cập là "các đối tượng được truyền qua các tài liệu tham khảo theo mặc định". Điều này không hoàn toàn đúng. Phần này điều chỉnh suy nghĩ chung đó bằng một số ví dụ.
Tham chiếu PHP là bí danh, cho phép hai biến khác nhau ghi vào cùng một giá trị. Trong PHP, một biến đối tượng không chứa chính đối tượng là giá trị. Nó chỉ chứa một định danh đối tượng cho phép người truy cập đối tượng tìm đối tượng thực tế. Khi một đối tượng được gửi bằng đối số, được trả về hoặc được gán cho một biến khác, các biến khác nhau không phải là bí danh: chúng giữ một bản sao của định danh, chỉ vào cùng một đối tượng.
Ví dụ #1 Tài liệu tham khảo và đối tượng
if[$box1 == $box2] echo 'equivalent';
2if[$box1 == $box2] echo 'equivalent';
3if[$box1 == $box2] echo 'equivalent';
4Ví dụ trên sẽ xuất ra:
miklcct tại gmail dot com ¶ ¶
12 năm trước
if[$box1 == $box2] echo 'equivalent';
5Ẩn danh ¶ ¶
11 năm trước
if[$box1 == $box2] echo 'equivalent';
6if[$box1 == $box2] echo 'equivalent';
7if[$box1 == $box2] echo 'equivalent';
8if[$box1 == $box2] echo 'equivalent';
9if[$box1 === $box2] echo 'exact same object!';
0if[$box1 === $box2] echo 'exact same object!';
1if[$box1 === $box2] echo 'exact same object!';
2if[$box1 === $box2] echo 'exact same object!';
3if[$box1 === $box2] echo 'exact same object!';
4Aaron Bond ¶ ¶
13 năm trước
if[$box1 === $box2] echo 'exact same object!';
5if[$box1 === $box2] echo 'exact same object!';
6if[$box1 === $box2] echo 'exact same object!';
7if[$box1 === $box2] echo 'exact same object!';
8if[$box1 === $box2] echo 'exact same object!';
9$courier = new PigeonPost['Avian Delivery Ltd'];$other_courier = $courier;0
$other_courier->name = 'Pigeon Post';echo $courier->name; // outputs "Pigeon Post"
$courier = new PigeonPost['Avian Delivery Ltd'];$other_courier = $courier;1
$other_courier->name = 'Pigeon Post';echo $courier->name; // outputs "Pigeon Post"
Kristof tại Viewranger dot com ¶
10 năm trước
$courier = new PigeonPost['Avian Delivery Ltd'];$other_courier = $courier;2
$other_courier->name = 'Pigeon Post';echo $courier->name; // outputs "Pigeon Post"
$courier = new PigeonPost['Avian Delivery Ltd'];$other_courier = $courier;3
$other_courier->name = 'Pigeon Post';echo $courier->name; // outputs "Pigeon Post"
$courier = new PigeonPost['Avian Delivery Ltd'];$other_courier = $courier;4
$other_courier->name = 'Pigeon Post';echo $courier->name; // outputs "Pigeon Post"
mjung tại poczta dot onet dot pl ¶ ¶
13 năm trước
$courier = new PigeonPost['Avian Delivery Ltd'];$other_courier = $courier;5
$other_courier->name = 'Pigeon Post';echo $courier->name; // outputs "Pigeon Post"
Kristof tại Viewranger dot com ¶
10 năm trước
$courier = new PigeonPost['Avian Delivery Ltd'];$other_courier = $courier;6
$other_courier->name = 'Pigeon Post';echo $courier->name; // outputs "Pigeon Post"
$courier = new PigeonPost['Avian Delivery Ltd'];$other_courier = $courier;7
$other_courier->name = 'Pigeon Post';echo $courier->name; // outputs "Pigeon Post"
$courier = new PigeonPost['Avian Delivery Ltd'];$other_courier = $courier;4
$other_courier->name = 'Pigeon Post';echo $courier->name; // outputs "Pigeon Post"
wassimamal121 tại hotmail dot com ¶
7 năm trước
$courier = new PigeonPost['Avian Delivery Ltd'];$other_courier = $courier;9
$other_courier->name = 'Pigeon Post';echo $courier->name; // outputs "Pigeon Post"
$courier = new PigeonPost['Avian Delivery Ltd'];$other_courier = clone $courier;0
$other_courier->name = 'Pigeon Post';echo $courier->name; // outputs "Avian Delivery Ltd"
$courier = new PigeonPost['Avian Delivery Ltd'];$other_courier = clone $courier;1
$other_courier->name = 'Pigeon Post';echo $courier->name; // outputs "Avian Delivery Ltd"
WBCarts tại Juno Dot Com ¶ ¶
14 năm trước
$courier = new PigeonPost['Avian Delivery Ltd'];$other_courier = clone $courier;2
$other_courier->name = 'Pigeon Post';echo $courier->name; // outputs "Avian Delivery Ltd"
$courier = new PigeonPost['Avian Delivery Ltd'];$other_courier = clone $courier;3
$other_courier->name = 'Pigeon Post';echo $courier->name; // outputs "Avian Delivery Ltd"
$courier = new PigeonPost['Avian Delivery Ltd'];$other_courier = clone $courier;4
$other_courier->name = 'Pigeon Post';echo $courier->name; // outputs "Avian Delivery Ltd"
$courier = new PigeonPost['Avian Delivery Ltd'];$other_courier = clone $courier;5
$other_courier->name = 'Pigeon Post';echo $courier->name; // outputs "Avian Delivery Ltd"
$courier = new PigeonPost['Avian Delivery Ltd'];$other_courier = clone $courier;6
$other_courier->name = 'Pigeon Post';echo $courier->name; // outputs "Avian Delivery Ltd"
Jon Whitener ¶ ¶
10 năm trước
$courier = new PigeonPost['Avian Delivery Ltd'];$other_courier = clone $courier;7
$other_courier->name = 'Pigeon Post';echo $courier->name; // outputs "Avian Delivery Ltd"
$courier = new PigeonPost['Avian Delivery Ltd'];$other_courier = clone $courier;8
$other_courier->name = 'Pigeon Post';echo $courier->name; // outputs "Avian Delivery Ltd"
$courier = new PigeonPost['Avian Delivery Ltd'];$other_courier = clone $courier;0
$other_courier->name = 'Pigeon Post';echo $courier->name; // outputs "Avian Delivery Ltd"
class Parcel0
{
protected $weight;
protected $destinationCountry;public function setWeight[$weight] {
echo "weight set to: " . $weight . "\n";
$this->weight = $weight;
return $this;
}public function setCountry[$country] {
echo "destination country is: " . $country . "\n";
$this->destinationCountry = $country;
return $this;
}
}$myParcel = new Parcel;
$myParcel->setWeight[5]->setCountry['Egypt'];
Ẩn danh ¶ ¶
11 năm trước
class Parcel1
{
protected $weight;
protected $destinationCountry;public function setWeight[$weight] {
echo "weight set to: " . $weight . "\n";
$this->weight = $weight;
return $this;
}public function setCountry[$country] {
$myParcel = new Parcel;
echo "destination country is: " . $country . "\n";
$this->destinationCountry = $country;
return $this;
}
}
$myParcel->setWeight[5]->setCountry['Egypt'];
class Parcel2
{
protected $weight;
protected $destinationCountry;public function setWeight[$weight] {
echo "weight set to: " . $weight . "\n";
$this->weight = $weight;
return $this;
}public function setCountry[$country] {
$myParcel = new Parcel;
echo "destination country is: " . $country . "\n";
$this->destinationCountry = $country;
return $this;
}
}
$myParcel->setWeight[5]->setCountry['Egypt'];
$courier = new PigeonPost['Avian Delivery Ltd'];$other_courier = $courier;0
$other_courier->name = 'Pigeon Post';echo $courier->name; // outputs "Pigeon Post"
class Parcel4
{
protected $weight;
protected $destinationCountry;public function setWeight[$weight] {
echo "weight set to: " . $weight . "\n";
$this->weight = $weight;
return $this;
}public function setCountry[$country] {
$myParcel = new Parcel;
echo "destination country is: " . $country . "\n";
$this->destinationCountry = $country;
return $this;
}
}
$myParcel->setWeight[5]->setCountry['Egypt'];
cesoid tại gmail dot com ¶
9 năm trước
class Parcel5
{
protected $weight;
protected $destinationCountry;public function setWeight[$weight] {
echo "weight set to: " . $weight . "\n";
$this->weight = $weight;
return $this;
}public function setCountry[$country] {
$myParcel = new Parcel;
echo "destination country is: " . $country . "\n";
$this->destinationCountry = $country;
return $this;
}
}
$myParcel->setWeight[5]->setCountry['Egypt'];
class Parcel6
{
protected $weight;
protected $destinationCountry;public function setWeight[$weight] {
echo "weight set to: " . $weight . "\n";
$this->weight = $weight;
return $this;
}public function setCountry[$country] {
$myParcel = new Parcel;
echo "destination country is: " . $country . "\n";
$this->destinationCountry = $country;
return $this;
}
}
$myParcel->setWeight[5]->setCountry['Egypt'];
class Parcel7
{
protected $weight;
protected $destinationCountry;public function setWeight[$weight] {
echo "weight set to: " . $weight . "\n";
$this->weight = $weight;
return $this;
}public function setCountry[$country] {
$myParcel = new Parcel;
echo "destination country is: " . $country . "\n";
$this->destinationCountry = $country;
return $this;
}
}
$myParcel->setWeight[5]->setCountry['Egypt'];
class Parcel8
{
protected $weight;
protected $destinationCountry;public function setWeight[$weight] {
echo "weight set to: " . $weight . "\n";
$this->weight = $weight;
return $this;
}public function setCountry[$country] {
$myParcel = new Parcel;
echo "destination country is: " . $country . "\n";
$this->destinationCountry = $country;
return $this;
}
}
$myParcel->setWeight[5]->setCountry['Egypt'];
$courier = new PigeonPost['Avian Delivery Ltd'];$other_courier = $courier;4
$other_courier->name = 'Pigeon Post';echo $courier->name; // outputs "Pigeon Post"
Ivan Bertona ¶ ¶
14 năm trước
reference
0
reference
1
reference
2
reference
3
reference
4Jon Whitener ¶ ¶
10 năm trước
reference
5
Ẩn danh ¶ ¶
11 năm trước
reference
6
reference
7
$courier = new PigeonPost['Avian Delivery Ltd'];$other_courier = $courier;0
$other_courier->name = 'Pigeon Post';echo $courier->name; // outputs "Pigeon Post"
reference
9cesoid tại gmail dot com ¶
9 năm trước
Objects are always passed by reference
0
Objects are always passed by reference
1
Objects are always passed by reference
2
Objects are always passed by reference
3
Objects are always passed by reference
4
Objects are always passed by reference
5
Objects are always passed by reference
6Ivan Bertona ¶ ¶
11 năm trước
Objects are always passed by reference
7
Objects are always passed by reference
8
$courier = new PigeonPost['Avian Delivery Ltd'];$other_courier = clone $courier;0
$other_courier->name = 'Pigeon Post';echo $courier->name; // outputs "Avian Delivery Ltd"
reference
0cesoid tại gmail dot com ¶
9 năm trước
reference
1
reference
2
reference
3
reference
4Ivan Bertona ¶ ¶
14 năm trước
reference
5
reference
6
reference
7
$courier = new PigeonPost['Avian Delivery Ltd'];$other_courier = clone $courier;5
$other_courier->name = 'Pigeon Post';echo $courier->name; // outputs "Avian Delivery Ltd"
reference
9