Nette\Utils\Reflection là một lớp tĩnh với các chức năng hữu ích để phản ánh PHP. Mục đích của nó là sửa lỗi trong các lớp gốc và để thống nhất hành vi trên các phiên bản PHP khác nhau
→
Tất cả các ví dụ giả sử bí danh lớp sau được xác định
use Nette\Utils\Reflection;
Tìm hiểu xem sự phản chiếu có quyền truy cập vào các bình luận PHPdoc hay không. Nhận xét có thể không khả dụng do bộ đệm opcode, xem ví dụ về lệnh
expandClassName[string $name, ReflectionClass $context]. chuỗi
Mở rộng
namespace Foo;
use Bar;
class DemoClass
{
// new Bar, new Baz
}
$context = new ReflectionClass[Foo\DemoClass::class];
Reflection::expandClassName['Bar', $context]; // 'Bar'
Reflection::expandClassName['Baz', $context]; // 'Foo\Baz'
0 của lớp thành tên đầy đủ trong ngữ cảnh của namespace Foo;
use Bar;
class DemoClass
{
// new Bar, new Baz
}
$context = new ReflectionClass[Foo\DemoClass::class];
Reflection::expandClassName['Bar', $context]; // 'Bar'
Reflection::expandClassName['Baz', $context]; // 'Foo\Baz'
1, tức là trong ngữ cảnh không gian tên của nó và các bí danh đã xác định. Do đó, nó trả về cách trình phân tích cú pháp PHP sẽ hiểu namespace Foo;
use Bar;
class DemoClass
{
// new Bar, new Baz
}
$context = new ReflectionClass[Foo\DemoClass::class];
Reflection::expandClassName['Bar', $context]; // 'Bar'
Reflection::expandClassName['Baz', $context]; // 'Foo\Baz'
0 nếu nó được viết trong phần thân của namespace Foo;
use Bar;
class DemoClass
{
// new Bar, new Baz
}
$context = new ReflectionClass[Foo\DemoClass::class];
Reflection::expandClassName['Bar', $context]; // 'Bar'
Reflection::expandClassName['Baz', $context]; // 'Foo\Baz'
1namespace Foo;
use Bar;
class DemoClass
{
// new Bar, new Baz
}
$context = new ReflectionClass[Foo\DemoClass::class];
Reflection::expandClassName['Bar', $context]; // 'Bar'
Reflection::expandClassName['Baz', $context]; // 'Foo\Baz'
getMethodDeclaringMethod[ReflectionMethod $method]. Phương thức phản ánh
Trả về một phản ánh của một phương thức có chứa một tuyên bố của
namespace Foo;
use Bar;
class DemoClass
{
// new Bar, new Baz
}
$context = new ReflectionClass[Foo\DemoClass::class];
Reflection::expandClassName['Bar', $context]; // 'Bar'
Reflection::expandClassName['Baz', $context]; // 'Foo\Baz'
4. Thông thường, mỗi phương thức là một khai báo riêng, nhưng phần thân của phương thức cũng có thể nằm trong một đặc điểm và dưới một tên khácVì PHP không cung cấp đủ thông tin để xác định khai báo thực tế, Nette sử dụng phương pháp phỏng đoán của riêng mình, phương pháp này đáng tin cậy
trait DemoTrait
{
function foo[]
{
}
}
class DemoClass
{
use DemoTrait {
DemoTrait::foo as foo2;
}
}
$method = new ReflectionMethod['DemoClass::foo2'];
Reflection::getMethodDeclaringMethod[$method]; // ReflectionMethod['DemoTrait::foo']
getParameterDefaultValue[ReflectionParameter $param]
Trả về giá trị mặc định của
namespace Foo;
use Bar;
class DemoClass
{
// new Bar, new Baz
}
$context = new ReflectionClass[Foo\DemoClass::class];
Reflection::expandClassName['Bar', $context]; // 'Bar'
Reflection::expandClassName['Baz', $context]; // 'Foo\Baz'
5 trong một hàm hoặc phương thức. Nếu nó là một hằng số, nó trả về giá trị của nó. Nếu tham số không có giá trị mặc định hoặc hằng số không thể giải quyết được, thì nó sẽ ném ra namespace Foo;
use Bar;
class DemoClass
{
// new Bar, new Baz
}
$context = new ReflectionClass[Foo\DemoClass::class];
Reflection::expandClassName['Bar', $context]; // 'Bar'
Reflection::expandClassName['Baz', $context]; // 'Foo\Baz'
6class DemoClass
{
function foo[$param = PDO::FETCH_BOTH]
{
}
}
$method = new ReflectionMethod['DemoClass::foo'];
Reflection::getParameterDefaultValue[$method->getParameters[][0]];
getParameterType[ReflectionParameter $param]. ?chuỗi
Trả về loại tham số của hàm hoặc phương thức
namespace Foo;
use Bar;
class DemoClass
{
// new Bar, new Baz
}
$context = new ReflectionClass[Foo\DemoClass::class];
Reflection::expandClassName['Bar', $context]; // 'Bar'
Reflection::expandClassName['Baz', $context]; // 'Foo\Baz'
5 và phân giải namespace Foo;
use Bar;
class DemoClass
{
// new Bar, new Baz
}
$context = new ReflectionClass[Foo\DemoClass::class];
Reflection::expandClassName['Bar', $context]; // 'Bar'
Reflection::expandClassName['Baz', $context]; // 'Foo\Baz'
8 và namespace Foo;
use Bar;
class DemoClass
{
// new Bar, new Baz
}
$context = new ReflectionClass[Foo\DemoClass::class];
Reflection::expandClassName['Bar', $context]; // 'Bar'
Reflection::expandClassName['Baz', $context]; // 'Foo\Baz'
9 thành tên lớp thực. Nếu tham số không có loại, nó sẽ trả về trait DemoTrait
{
function foo[]
{
}
}
class DemoClass
{
use DemoTrait {
DemoTrait::foo as foo2;
}
}
$method = new ReflectionMethod['DemoClass::foo2'];
Reflection::getMethodDeclaringMethod[$method]; // ReflectionMethod['DemoTrait::foo']
0, nếu nó có loại hợp hoặc giao nhau, nó sẽ trả về trait DemoTrait
{
function foo[]
{
}
}
class DemoClass
{
use DemoTrait {
DemoTrait::foo as foo2;
}
}
$method = new ReflectionMethod['DemoClass::foo2'];
Reflection::getMethodDeclaringMethod[$method]; // ReflectionMethod['DemoTrait::foo']
1class DemoClass
{
function foo[self $param]
{
}
}
$method = new ReflectionMethod['DemoClass::foo'];
Reflection::getReturnType[$method->getParameters[][0]]; // 'DemoClass'
Phương thức này được tạo khi PHP không có các kiểu liên kết và giao nhau. Nó được thay thế bằng phương pháp