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
và
0
__construct[$o, $m]
1
{
parent::__construct[$o, $m];
$this->visibility = Reflection::getModifierNames[$this->getModifiers[]];
}
}/**
* Demo class #1
*
*/
class T {
protected function x[] {}
}/**
* Demo class #2
*
*/
class U extends T {
function x[] {}
}// Print out information
var_dump[new My_Reflection_Method['U', 'x']];
?>
__construct[$o, $m]
2
{
parent::__construct[$o, $m];
$this->visibility = Reflection::getModifierNames[$this->getModifiers[]];
}
}/**
* Demo class #1
*
*/
class T {
protected function x[] {}
}/**
* Demo class #2
*
*/
class U extends T {
function x[] {}
}// Print out information
var_dump[new My_Reflection_Method['U', 'x']];
?>
__construct[$o, $m]
{
parent::__construct[$o, $m];
$this->visibility = Reflection::getModifierNames[$this->getModifiers[]];
}
}/**
* Demo class #1
*
*/
class T {
protected function x[] {}
}/**
* Demo class #2
*
*/
class U extends T {
function x[] {}
}// Print out information
var_dump[new My_Reflection_Method['U', 'x']];
?>
35 năm trước ¶
Jackdracona tại msn dot com ¶
__construct[$o, $m]
4
{
parent::__construct[$o, $m];
$this->visibility = Reflection::getModifierNames[$this->getModifiers[]];
}
}/**
* Demo class #1
*
*/
class T {
protected function x[] {}
}/**
* Demo class #2
*
*/
class U extends T {
function x[] {}
}// Print out information
var_dump[new My_Reflection_Method['U', 'x']];
?>
__construct[$o, $m]
5
{
parent::__construct[$o, $m];
$this->visibility = Reflection::getModifierNames[$this->getModifiers[]];
}
}/**
* Demo class #1
*
*/
class T {
protected function x[] {}
}/**
* Demo class #2
*
*/
class U extends T {
function x[] {}
}// Print out information
var_dump[new My_Reflection_Method['U', 'x']];
?>
__construct[$o, $m]
6
{
parent::__construct[$o, $m];
$this->visibility = Reflection::getModifierNames[$this->getModifiers[]];
}
}/**
* Demo class #1
*
*/
class T {
protected function x[] {}
}/**
* Demo class #2
*
*/
class U extends T {
function x[] {}
}// Print out information
var_dump[new My_Reflection_Method['U', 'x']];
?>
__construct[$o, $m]
7
{
parent::__construct[$o, $m];
$this->visibility = Reflection::getModifierNames[$this->getModifiers[]];
}
}/**
* Demo class #1
*
*/
class T {
protected function x[] {}
}/**
* Demo class #2
*
*/
class U extends T {
function x[] {}
}// Print out information
var_dump[new My_Reflection_Method['U', 'x']];
?>
__construct[$o, $m]
8
{
parent::__construct[$o, $m];
$this->visibility = Reflection::getModifierNames[$this->getModifiers[]];
}
}/**
* Demo class #1
*
*/
class T {
protected function x[] {}
}/**
* Demo class #2
*
*/
class U extends T {
function x[] {}
}// Print out information
var_dump[new My_Reflection_Method['U', 'x']];
?>
__construct[$o, $m]
{
parent::__construct[$o, $m];
$this->visibility = Reflection::getModifierNames[$this->getModifiers[]];
}
}/**
* Demo class #1
*
*/
class T {
protected function x[] {}
}/**
* Demo class #2
*
*/
class U extends T {
function x[] {}
}// Print out information
var_dump[new My_Reflection_Method['U', 'x']];
?>
912 năm trước ¶
Mohammad Istanbouly ¶
Fatal error: Internal error: Failed to retrieve the reflection object
0
Fatal error: Internal error: Failed to retrieve the reflection object
1
Fatal error: Internal error: Failed to retrieve the reflection object
2
Fatal error: Internal error: Failed to retrieve the reflection object
3
Fatal error: Internal error: Failed to retrieve the reflection object
45 năm trước ¶
Strata_ranger tại hotmail dot com
Fatal error: Internal error: Failed to retrieve the reflection object
5
Fatal error: Internal error: Failed to retrieve the reflection object
6
Fatal error: Internal error: Failed to retrieve the reflection object
7
__construct[$o, $m]
8
{
parent::__construct[$o, $m];
$this->visibility = Reflection::getModifierNames[$this->getModifiers[]];
}
}/**
* Demo class #1
*
*/
class T {
protected function x[] {}
}/**
* Demo class #2
*
*/
class U extends T {
function x[] {}
}// Print out information
var_dump[new My_Reflection_Method['U', 'x']];
?>
Fatal error: Internal error: Failed to retrieve the reflection object
9Akashwebdev 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]
8
{
parent::__construct[$o, $m];
$this->visibility = Reflection::getModifierNames[$this->getModifiers[]];
}
}/**
* Demo class #1
*
*/
class T {
protected function x[] {}
}/**
* Demo class #2
*
*/
class U extends T {
function x[] {}
}// Print out information
var_dump[new My_Reflection_Method['U', 'x']];
?>
Extending class ReflectionFunction to get source code of function
3Jarrod 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
8Niemans tại pbsolo dot nl ¶ ¶
2 năm trước
Extending class ReflectionFunction to get source code of function
9