Hướng dẫn what is the purpose of $this and extend in php? - mục đích của $this và mở rộng trong php là gì?

Nếu bạn muốn tạo các phiên bản chuyên dụng của các lớp tích hợp [giả sử, để tạo HTML màu khi được xuất, có các biến thành viên dễ truy cập thay vì phương thức hoặc có phương thức tiện ích], bạn có thể tiếp tục và mở rộng chúng.

Ví dụ #1 mở rộng các lớp học tích hợp

Ví dụ trên sẽ xuất ra một cái gì đó tương tự như:

object[My_Reflection_Method]#1 [3] {
  ["visibility"]=>
  array[1] {
    [0]=>
    string[6] "public"
  }
  ["name"]=>
  string[1] "x"
  ["class"]=>
  string[1] "U"
}

Thận trọng

Nếu bạn đang ghi đè lên hàm tạo, hãy nhớ gọi cho hàm tạo của cha mẹ trước khi bất kỳ mã nào bạn chèn. Không làm như vậy sẽ dẫn đến những điều sau: Fatal error: Internal error: Failed to retrieve the reflection object

Khelaz tại gmail dot com ¶

11 năm trước

Extending class ReflectionFunction to get source code of function

Kế thừa là một nguyên tắc lập trình được thiết lập tốt và PHP sử dụng nguyên tắc này trong mô hình đối tượng của nó. Nguyên tắc này sẽ ảnh hưởng đến cách nhiều lớp và đối tượng liên quan đến nhau.

Ví dụ, khi mở rộng một lớp, lớp con kế thừa tất cả các phương thức, thuộc tính và hằng số được bảo vệ và được bảo vệ từ lớp cha. Trừ khi một lớp ghi đè các phương pháp đó, họ sẽ giữ lại chức năng ban đầu của chúng.

Điều này rất hữu ích để xác định và trừu tượng hóa chức năng và cho phép thực hiện chức năng bổ sung trong các đối tượng tương tự mà không cần phải thực hiện lại tất cả các chức năng được chia sẻ.

Các phương pháp riêng tư của lớp cha không thể truy cập được vào lớp trẻ. Do đó, các lớp trẻ em có thể tự tái tạo một phương thức riêng tư mà không quan tâm đến các quy tắc thừa kế bình thường. Tuy nhiên, trước Php 8.0.0, các hạn chế final 0

__construct[$o$m]
    {
        
parent::__construct[$o$m];
        
$this->visibility Reflection::getModifierNames[$this->getModifiers[]];
    }
}
/**
 * Demo class #1
 *
 */
class {
    protected function 
x[] {}
}
/**
 * Demo class #2
 *
 */
class extends {
    function 
x[] {}
}
// Print out information
var_dump[new My_Reflection_Method['U''x']];
?>
1

__construct[$o$m]
    {
        
parent::__construct[$o$m];
        
$this->visibility Reflection::getModifierNames[$this->getModifiers[]];
    }
}
/**
 * Demo class #1
 *
 */
class {
    protected function 
x[] {}
}
/**
 * Demo class #2
 *
 */
class extends {
    function 
x[] {}
}
// Print out information
var_dump[new My_Reflection_Method['U''x']];
?>
2

__construct[$o$m]
    {
        
parent::__construct[$o$m];
        
$this->visibility Reflection::getModifierNames[$this->getModifiers[]];
    }
}
/**
 * Demo class #1
 *
 */
class {
    protected function 
x[] {}
}
/**
 * Demo class #2
 *
 */
class extends {
    function 
x[] {}
}
// Print out information
var_dump[new My_Reflection_Method['U''x']];
?>
3

5 năm trước

Jackdracona tại msn dot com ¶

__construct[$o$m]
    {
        
parent::__construct[$o$m];
        
$this->visibility Reflection::getModifierNames[$this->getModifiers[]];
    }
}
/**
 * Demo class #1
 *
 */
class {
    protected function 
x[] {}
}
/**
 * Demo class #2
 *
 */
class extends {
    function 
x[] {}
}
// Print out information
var_dump[new My_Reflection_Method['U''x']];
?>
4

__construct[$o$m]
    {
        
parent::__construct[$o$m];
        
$this->visibility Reflection::getModifierNames[$this->getModifiers[]];
    }
}
/**
 * Demo class #1
 *
 */
class {
    protected function 
x[] {}
}
/**
 * Demo class #2
 *
 */
class extends {
    function 
x[] {}
}
// Print out information
var_dump[new My_Reflection_Method['U''x']];
?>
5

__construct[$o$m]
    {
        
parent::__construct[$o$m];
        
$this->visibility Reflection::getModifierNames[$this->getModifiers[]];
    }
}
/**
 * Demo class #1
 *
 */
class {
    protected function 
x[] {}
}
/**
 * Demo class #2
 *
 */
class extends {
    function 
x[] {}
}
// Print out information
var_dump[new My_Reflection_Method['U''x']];
?>
6

__construct[$o$m]
    {
        
parent::__construct[$o$m];
        
$this->visibility Reflection::getModifierNames[$this->getModifiers[]];
    }
}
/**
 * Demo class #1
 *
 */
class {
    protected function 
x[] {}
}
/**
 * Demo class #2
 *
 */
class extends {
    function 
x[] {}
}
// Print out information
var_dump[new My_Reflection_Method['U''x']];
?>
7

__construct[$o$m]
    {
        
parent::__construct[$o$m];
        
$this->visibility Reflection::getModifierNames[$this->getModifiers[]];
    }
}
/**
 * Demo class #1
 *
 */
class {
    protected function 
x[] {}
}
/**
 * Demo class #2
 *
 */
class extends {
    function 
x[] {}
}
// Print out information
var_dump[new My_Reflection_Method['U''x']];
?>
8

__construct[$o$m]
    {
        
parent::__construct[$o$m];
        
$this->visibility Reflection::getModifierNames[$this->getModifiers[]];
    }
}
/**
 * Demo class #1
 *
 */
class {
    protected function 
x[] {}
}
/**
 * Demo class #2
 *
 */
class extends {
    function 
x[] {}
}
// Print out information
var_dump[new My_Reflection_Method['U''x']];
?>
9

12 năm trước

Mohammad Istanbouly ¶

Fatal error: Internal error: Failed to retrieve the reflection object0

Fatal error: Internal error: Failed to retrieve the reflection object1

Fatal error: Internal error: Failed to retrieve the reflection object2

Fatal error: Internal error: Failed to retrieve the reflection object3

Fatal error: Internal error: Failed to retrieve the reflection object4

5 năm trước

Strata_ranger tại hotmail dot com

Fatal error: Internal error: Failed to retrieve the reflection object5

Fatal error: Internal error: Failed to retrieve the reflection object6

Fatal error: Internal error: Failed to retrieve the reflection object7

__construct[$o$m]
    {
        
parent::__construct[$o$m];
        
$this->visibility Reflection::getModifierNames[$this->getModifiers[]];
    }
}
/**
 * Demo class #1
 *
 */
class {
    protected function 
x[] {}
}
/**
 * Demo class #2
 *
 */
class extends {
    function 
x[] {}
}
// Print out information
var_dump[new My_Reflection_Method['U''x']];
?>
8

Fatal error: Internal error: Failed to retrieve the reflection object9

Akashwebdev tại gmail dot com ¶

7 năm trước

Extending class ReflectionFunction to get source code of function 0

Extending class ReflectionFunction to get source code of function 1

__construct[$o$m]
    {
        
parent::__construct[$o$m];
        
$this->visibility Reflection::getModifierNames[$this->getModifiers[]];
    }
}
/**
 * Demo class #1
 *
 */
class {
    protected function 
x[] {}
}
/**
 * Demo class #2
 *
 */
class extends {
    function 
x[] {}
}
// Print out information
var_dump[new My_Reflection_Method['U''x']];
?>
8

Extending class ReflectionFunction to get source code of function 3

Jarrod tại Squarecrow Dot Com ¶

13 năm trước

Extending class ReflectionFunction to get source code of function 4

Extending class ReflectionFunction to get source code of function 5

Extending class ReflectionFunction to get source code of function 6

Extending class ReflectionFunction to get source code of function 7

Extending class ReflectionFunction to get source code of function 8

Niemans tại pbsolo dot nl ¶

2 năm trước

Extending class ReflectionFunction to get source code of function 9

Bài Viết Liên Quan

Chủ Đề