Bạn có thể sử dụng toán tử delete
để loại bỏ hoàn toàn các thuộc tính khỏi đối tượng JavaScript. Xóa là cách duy nhất để thực sự xóa một thuộc tính khỏi một đối tượng
Đặt thuộc tính thành undefined
hoặc null
chỉ thay đổi giá trị của thuộc tính. Nó không xóa thuộc tính khỏi đối tượng. Hãy cùng xem ví dụ sau
Since unset[] is a language construct, it cannot be passed anything other than a variable. It's sole purpose is to "unset" this variable, ie. to remove it from the current scope and destroy it's associated data. This is true especially for reference variables, where not the actual value is destroyed but the reference to that value. This is why you can't wrap 'unset[]' in a user defined function: You would either unset a copy of the data if the parameter is passed by value, or you would just unset the reference variable within the functions scope if the parameter is passed by reference. There is no workaround for that, as you cannot pass 'scope' to a function in PHP. Such a function can only work for variables that exist in a common or global scope [compare 'unset[$_GLOBALS[variable]]'].
I don't know how PHP handles garbage collection internally, but I guess this behavior can result in a huge memory leak: if a value variable goes out of scope with a second variable still holding a reference to the in-memory value, then unsetting that reference would still hold the value in memory but potentially unset the last reference to that in-memory data, hence: occupied memory that is rendered useless as you cannot reference it anymore.
Có thể sửa đổi hành vi của var_dump[]
bằng cách khai báo một phương thức __debugInfo[]
trong lớp xác định đối tượng mà bạn đang kết xuất. Phương thức kỳ diệu này sẽ được gọi tự động khi PHP gặp lệnh gọi tới var_dump[]
hoặc
public function on[$name, $handler, $data = null, $append = true]
{
$this->ensureBehaviors[];
if [strpos[$name, '*'] !== false] {
if [$append || empty[$this->_eventWildcards[$name]]] {
$this->_eventWildcards[$name][] = [$handler, $data];
} else {
array_unshift[$this->_eventWildcards[$name], [$handler, $data]];
}
return;
}
if [$append || empty[$this->_events[$name]]] {
$this->_events[$name][] = [$handler, $data];
} else {
array_unshift[$this->_events[$name], [$handler, $data]];
}
}
1 với một đối tượng của lớp này làm mục tiêuVí dụ của tôi, tôi chỉ muốn xóa một thuộc tính có tên là “client” khỏi đối tượng, vì vậy mã trông như thế này
public function __debugInfo[] { // remove the $client property because the output is HUGE $result = get_object_vars[$this]; unset[$result['client']]; return $result; }
public function on[$name, $handler, $data = null, $append = true]
{
$this->ensureBehaviors[];
if [strpos[$name, '*'] !== false] {
if [$append || empty[$this->_eventWildcards[$name]]] {
$this->_eventWildcards[$name][] = [$handler, $data];
} else {
array_unshift[$this->_eventWildcards[$name], [$handler, $data]];
}
return;
}
if [$append || empty[$this->_events[$name]]] {
$this->_events[$name][] = [$handler, $data];
} else {
array_unshift[$this->_events[$name], [$handler, $data]];
}
}
2 trả về tất cả các thuộc tính trong một mảng mà sau đó chúng ta có thể thay đổi trước khi trả về nó và đó là giá trị trả về được sử dụng bởi var_dump[]
. Các phần tử mảng vẫn được kết xuất dưới dạng thuộc tính đối tượng, nhưng không có thuộc tính lớn mà tôi muốn tránh Một mảng có 4 đối tượng được khai báo và gán cho biến ‘objectarray’. Ở đây, chúng tôi muốn xóa đối tượng khỏi chỉ mục 2, đối tượng này cũng được khai báo với biến có tên là 'chỉ mục'. Vòng lặp foreach được sử dụng để duyệt qua mảng và khi giá trị chỉ mục trong quá trình duyệt khớp với chỉ mục từ nơi giá trị cần được xóa, hàm 'unset' được gọi trên phần tử đó và các phần tử còn lại được trả về dưới dạng đầu ra
Toán tử xóa được thiết kế để sử dụng trên các thuộc tính đối tượng. Nó không ảnh hưởng đến các biến hoặc chức năng
Ghi chú. Không nên sử dụng toán tử xóa trên các thuộc tính đối tượng JavaScript được xác định trước. Nó có thể làm hỏng ứng dụng của bạn
public function on[$name, $handler, $data = null, $append = true]
{
$this->ensureBehaviors[];
if [strpos[$name, '*'] !== false] {
if [$append || empty[$this->_eventWildcards[$name]]] {
$this->_eventWildcards[$name][] = [$handler, $data];
} else {
array_unshift[$this->_eventWildcards[$name], [$handler, $data]];
}
return;
}
if [$append || empty[$this->_events[$name]]] {
$this->_events[$name][] = [$handler, $data];
} else {
array_unshift[$this->_events[$name], [$handler, $data]];
}
}
Kiểm tra hướng dẫn cho
public function on[$name, $handler, $data = null, $append = true]
{
$this->ensureBehaviors[];
if [strpos[$name, '*'] !== false] {
if [$append || empty[$this->_eventWildcards[$name]]] {
$this->_eventWildcards[$name][] = [$handler, $data];
} else {
array_unshift[$this->_eventWildcards[$name], [$handler, $data]];
}
return;
}
if [$append || empty[$this->_events[$name]]] {
$this->_events[$name][] = [$handler, $data];
} else {
array_unshift[$this->_events[$name], [$handler, $data]];
}
}
4. Nó trả về một đối tượng, không phải một mảng đối tượngTôi đoán bạn muốn một cái gì đó như thế này
$results = mysql_query["SELECT * FROM sidemenu WHERE `menu_id`='".$menu."' ORDER BY `id` ASC LIMIT 1", $con];
$sidemenus = array[];
while [$sidemenu = mysql_fetch_object[$results]] {
$sidemenus[] = $sidemenu;
}
Tôi có thể đề nghị bạn xem PDO.
public function on[$name, $handler, $data = null, $append = true]
{
$this->ensureBehaviors[];
if [strpos[$name, '*'] !== false] {
if [$append || empty[$this->_eventWildcards[$name]]] {
$this->_eventWildcards[$name][] = [$handler, $data];
} else {
array_unshift[$this->_eventWildcards[$name], [$handler, $data]];
}
return;
}
if [$append || empty[$this->_events[$name]]] {
$this->_events[$name][] = [$handler, $data];
} else {
array_unshift[$this->_events[$name], [$handler, $data]];
}
}
5 làm những gì bạn cho rằng public function on[$name, $handler, $data = null, $append = true]
{
$this->ensureBehaviors[];
if [strpos[$name, '*'] !== false] {
if [$append || empty[$this->_eventWildcards[$name]]] {
$this->_eventWildcards[$name][] = [$handler, $data];
} else {
array_unshift[$this->_eventWildcards[$name], [$handler, $data]];
}
return;
}
if [$append || empty[$this->_events[$name]]] {
$this->_events[$name][] = [$handler, $data];
} else {
array_unshift[$this->_events[$name], [$handler, $data]];
}
}
4 sẽ làm