Hướng dẫn php pass object to function by reference - php chuyển đối tượng sang chức năng bằng cách tham chiếu

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) {
echo "destination country is: " . $country . "\n";
$this->destinationCountry = $country;
return $this;
}
}

$myParcel = new Parcel;
$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';
2

if($box1 == $box2) echo 'equivalent';
3

if($box1 == $box2) echo 'equivalent';
4

Ví 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';
6

if($box1 == $box2) echo 'equivalent';
7

if($box1 == $box2) echo 'equivalent';
8

if($box1 == $box2) echo 'equivalent';
9

if($box1 === $box2) echo 'exact same object!';
0

if($box1 === $box2) echo 'exact same object!';
1

if($box1 === $box2) echo 'exact same object!';
2

if($box1 === $box2) echo 'exact same object!';
3

if($box1 === $box2) echo 'exact same object!';
4

Aaron Bond ¶

13 năm trước

if($box1 === $box2) echo 'exact same object!';
5

if($box1 === $box2) echo 'exact same object!';
6

if($box1 === $box2) echo 'exact same object!';
7

if($box1 === $box2) echo 'exact same object!';
8

if($box1 === $box2) echo 'exact same object!';
9

$courier = new PigeonPost('Avian Delivery Ltd');$other_courier = $courier;
$other_courier->name = 'Pigeon Post';
echo $courier->name; // outputs "Pigeon Post"
0

$courier = new PigeonPost('Avian Delivery Ltd');$other_courier = $courier;
$other_courier->name = 'Pigeon Post';
echo $courier->name; // outputs "Pigeon Post"
1

Kristof tại Viewranger dot com

10 năm trước

$courier = new PigeonPost('Avian Delivery Ltd');$other_courier = $courier;
$other_courier->name = 'Pigeon Post';
echo $courier->name; // outputs "Pigeon Post"
2

$courier = new PigeonPost('Avian Delivery Ltd');$other_courier = $courier;
$other_courier->name = 'Pigeon Post';
echo $courier->name; // outputs "Pigeon Post"
3

$courier = new PigeonPost('Avian Delivery Ltd');$other_courier = $courier;
$other_courier->name = 'Pigeon Post';
echo $courier->name; // outputs "Pigeon Post"
4

mjung tại poczta dot onet dot pl ¶

13 năm trước

$courier = new PigeonPost('Avian Delivery Ltd');$other_courier = $courier;
$other_courier->name = 'Pigeon Post';
echo $courier->name; // outputs "Pigeon Post"
5

Kristof tại Viewranger dot com

10 năm trước

$courier = new PigeonPost('Avian Delivery Ltd');$other_courier = $courier;
$other_courier->name = 'Pigeon Post';
echo $courier->name; // outputs "Pigeon Post"
6

$courier = new PigeonPost('Avian Delivery Ltd');$other_courier = $courier;
$other_courier->name = 'Pigeon Post';
echo $courier->name; // outputs "Pigeon Post"
7

$courier = new PigeonPost('Avian Delivery Ltd');$other_courier = $courier;
$other_courier->name = 'Pigeon Post';
echo $courier->name; // outputs "Pigeon Post"
4

wassimamal121 tại hotmail dot com

7 năm trước

$courier = new PigeonPost('Avian Delivery Ltd');$other_courier = $courier;
$other_courier->name = 'Pigeon Post';
echo $courier->name; // outputs "Pigeon Post"
9

$courier = new PigeonPost('Avian Delivery Ltd');$other_courier = clone $courier;
$other_courier->name = 'Pigeon Post';
echo $courier->name; // outputs "Avian Delivery Ltd"
0

$courier = new PigeonPost('Avian Delivery Ltd');$other_courier = clone $courier;
$other_courier->name = 'Pigeon Post';
echo $courier->name; // outputs "Avian Delivery Ltd"
1

WBCarts tại Juno Dot Com ¶

14 năm trước

$courier = new PigeonPost('Avian Delivery Ltd');$other_courier = clone $courier;
$other_courier->name = 'Pigeon Post';
echo $courier->name; // outputs "Avian Delivery Ltd"
2

$courier = new PigeonPost('Avian Delivery Ltd');$other_courier = clone $courier;
$other_courier->name = 'Pigeon Post';
echo $courier->name; // outputs "Avian Delivery Ltd"
3

$courier = new PigeonPost('Avian Delivery Ltd');$other_courier = clone $courier;
$other_courier->name = 'Pigeon Post';
echo $courier->name; // outputs "Avian Delivery Ltd"
4

$courier = new PigeonPost('Avian Delivery Ltd');$other_courier = clone $courier;
$other_courier->name = 'Pigeon Post';
echo $courier->name; // outputs "Avian Delivery Ltd"
5

$courier = new PigeonPost('Avian Delivery Ltd');$other_courier = clone $courier;
$other_courier->name = 'Pigeon Post';
echo $courier->name; // outputs "Avian Delivery Ltd"
6

Jon Whitener ¶

10 năm trước

$courier = new PigeonPost('Avian Delivery Ltd');$other_courier = clone $courier;
$other_courier->name = 'Pigeon Post';
echo $courier->name; // outputs "Avian Delivery Ltd"
7

$courier = new PigeonPost('Avian Delivery Ltd');$other_courier = clone $courier;
$other_courier->name = 'Pigeon Post';
echo $courier->name; // outputs "Avian Delivery Ltd"
8

$courier = new PigeonPost('Avian Delivery Ltd');$other_courier = clone $courier;
$other_courier->name = 'Pigeon Post';
echo $courier->name; // outputs "Avian Delivery Ltd"
0

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) {
echo "destination country is: " . $country . "\n";
$this->destinationCountry = $country;
return $this;
}
}

$myParcel = new Parcel;
$myParcel->setWeight(5)->setCountry('Egypt');
0

Ẩn danh ¶

11 năm trước

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) {
echo "destination country is: " . $country . "\n";
$this->destinationCountry = $country;
return $this;
}
}

$myParcel = new Parcel;
$myParcel->setWeight(5)->setCountry('Egypt');
1

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) {
echo "destination country is: " . $country . "\n";
$this->destinationCountry = $country;
return $this;
}
}

$myParcel = new Parcel;
$myParcel->setWeight(5)->setCountry('Egypt');
2

$courier = new PigeonPost('Avian Delivery Ltd');$other_courier = $courier;
$other_courier->name = 'Pigeon Post';
echo $courier->name; // outputs "Pigeon Post"
0

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) {
echo "destination country is: " . $country . "\n";
$this->destinationCountry = $country;
return $this;
}
}

$myParcel = new Parcel;
$myParcel->setWeight(5)->setCountry('Egypt');
4

cesoid tại gmail dot com

9 năm trước

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) {
echo "destination country is: " . $country . "\n";
$this->destinationCountry = $country;
return $this;
}
}

$myParcel = new Parcel;
$myParcel->setWeight(5)->setCountry('Egypt');
5

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) {
echo "destination country is: " . $country . "\n";
$this->destinationCountry = $country;
return $this;
}
}

$myParcel = new Parcel;
$myParcel->setWeight(5)->setCountry('Egypt');
6

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) {
echo "destination country is: " . $country . "\n";
$this->destinationCountry = $country;
return $this;
}
}

$myParcel = new Parcel;
$myParcel->setWeight(5)->setCountry('Egypt');
7

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) {
echo "destination country is: " . $country . "\n";
$this->destinationCountry = $country;
return $this;
}
}

$myParcel = new Parcel;
$myParcel->setWeight(5)->setCountry('Egypt');
8

$courier = new PigeonPost('Avian Delivery Ltd');$other_courier = $courier;
$other_courier->name = 'Pigeon Post';
echo $courier->name; // outputs "Pigeon Post"
4

Ivan Bertona ¶

14 năm trước

reference0

reference1

reference2

reference3

reference4

Jon Whitener ¶

10 năm trước

reference5

Ẩn danh ¶

11 năm trước

reference6

reference7

$courier = new PigeonPost('Avian Delivery Ltd');$other_courier = $courier;
$other_courier->name = 'Pigeon Post';
echo $courier->name; // outputs "Pigeon Post"
0

reference9

cesoid tại gmail dot com

9 năm trước

Objects are always passed by reference0

Objects are always passed by reference1

Objects are always passed by reference2

Objects are always passed by reference3

Objects are always passed by reference4

Objects are always passed by reference5

Objects are always passed by reference6

Ivan Bertona ¶

11 năm trước

Objects are always passed by reference7

Objects are always passed by reference8

$courier = new PigeonPost('Avian Delivery Ltd');$other_courier = clone $courier;
$other_courier->name = 'Pigeon Post';
echo $courier->name; // outputs "Avian Delivery Ltd"
0

reference0

cesoid tại gmail dot com

9 năm trước

reference1

reference2

reference3

reference4

Ivan Bertona ¶

14 năm trước

reference5

reference6

reference7

$courier = new PigeonPost('Avian Delivery Ltd');$other_courier = clone $courier;
$other_courier->name = 'Pigeon Post';
echo $courier->name; // outputs "Avian Delivery Ltd"
5

reference9

Bạn có thể vượt qua một đối tượng bằng cách tham khảo?

Giá trị của đối tượng có thể thay đổi có thể được thay đổi khi nó được chuyển sang phương thức. Giá trị của một đối tượng bất biến không thể thay đổi, ngay cả khi nó được truyền một giá trị mới. Vượt qua giá trị của người Viking đề cập đến việc chuyển một bản sao của giá trị. Vượt qua bằng cách tham chiếu, đề cập đến việc truyền tham chiếu thực của biến trong bộ nhớ.“Passing by reference” refers to passing the real reference of the variable in memory.

Có đi qua bằng tham chiếu PHP nhanh hơn không?

Trong thực tế, trong hầu hết các kịch bản đi qua giá trị là nhanh hơn và ít bộ nhớ hơn so với truyền qua tham chiếu. Công cụ Zend, lõi của PHP, sử dụng cơ chế tối ưu hóa sao chép không có chữ viết, không tạo ra một bản sao của một biến cho đến khi nó được sửa đổi.in most scenarios passing by value is faster and less memory intensive than passing by reference. The Zend Engine, PHP's core, uses a copy-on-write optimization mechanism that does not create a copy of a variable until it is modified.

Sự khác biệt giữa và $$ trong PHP là gì?

PHP |$ vs $$ toán tử $ var_name = "Hello World!";$ Var_name là một biến bình thường được sử dụng để lưu trữ một giá trị.Nó có thể lưu trữ bất kỳ giá trị nào như Integer, Float, Char, String, v.v. Mặt khác, $$ var_name được gọi là biến tham chiếu trong đó $ var_name là một biến bình thường.the $$var_name is known as reference variable where $var_name is a normal variable.

Php mảng có vượt qua tham chiếu không?

Liên quan đến câu hỏi đầu tiên của bạn, mảng được truyền qua tham chiếu trừ khi nó được sửa đổi trong phương thức / hàm bạn đang gọi.Nếu bạn cố gắng sửa đổi mảng trong phương thức / hàm, một bản sao của nó được tạo trước tiên và sau đó chỉ có bản sao được sửa đổi.the array is passed by reference UNLESS it is modified within the method / function you're calling. If you attempt to modify the array within the method / function, a copy of it is made first, and then only the copy is modified.