(PHP 5 >= 5.3.0, PHP 7, PHP 8)
ReflectionClass::getNamespaceName — Gets namespace name — Gets namespace name
Description
publicReflectionClass::getNamespaceName(): string ReflectionClass::getNamespaceName(): string
Parameters
This function has no parameters.
Return Values
The namespace name.
Examples
Example #1 ReflectionClass::getNamespaceName() exampleReflectionClass::getNamespaceName() example
var_dump(get_class($user)); //App\Models\User var_dump(get_class_method($user)); //Method A //Method B //Method C //Something else 9
class User { protected function getUsername() { //.... } public function __get($param) { $method = 'get' . ucfirst($param); if (method_exists($this, $method)) { return $this->{$method}(); } } } 0
class User { protected function getUsername() { //.... } public function __get($param) { $method = 'get' . ucfirst($param); if (method_exists($this, $method)) { return $this->{$method}(); } } } 1The above example will output:
bool(false) string(8) "stdClass" string(0) "" string(8) "stdClass" bool(true) string(7) "A\B\Foo" string(3) "A\B" string(3) "Foo"
francois ¶ ¶
11 years ago
class User { protected function getUsername() { //.... } public function __get($param) { $method = 'get' . ucfirst($param); if (method_exists($this, $method)) { return $this->{$method}(); } } } 2
Reflection là gì?
Ngắn gọn nhất thì có thể nói Reflection cung cấp khả năng phân tích cấu trúc bên trong một class bao gồm các: method, property, const, comment và thay đổi (modify) chúng.
Nó dùng để làm gì?
Thật ra là mình cũng ít (không) khi nào dùng đến cái này lắm, nhưng qua tìm hiểu thì thấy nó khá là hữu ích, có thể thay đổi cách làm 1 số chuyện của mình.
Ví dụ đọc code của ai đó mà không biết cái biến này là gì, 1 object hay 1 số, 1 string thì có thể dùng hàm cơ bản của PHP là class User { protected function getUsername() { //.... } public function __get($param) { $method = 'get' . ucfirst($param); if (method_exists($this, $method)) { return $this->{$method}(); } } } 3 và class User { protected function getUsername() { //.... } public function __get($param) { $method = 'get' . ucfirst($param); if (method_exists($this, $method)) { return $this->{$method}(); } } } 4.
Tiếp theo là chúng ta có thể sử dụng Reflection để tạo tài liệu bằng cách get comment của 1 class nào đó, rồi kiểm tra từng method, constructor và class đó để xác định những gì diễn ra đối với đầu vào và đầu ra.
Các hàm Reflection thông dụng
Ví dụ ta dùng class User { protected function getUsername() { //.... } public function __get($param) { $method = 'get' . ucfirst($param); if (method_exists($this, $method)) { return $this->{$method}(); } } } 3 và class User { protected function getUsername() { //.... } public function __get($param) { $method = 'get' . ucfirst($param); if (method_exists($this, $method)) { return $this->{$method}(); } } } 6:
var_dump(get_class($user)); //App\Models\User var_dump(get_class_method($user)); //Method A //Method B //Method C //Something elseclass User { protected function getUsername() { //.... } public function __get($param) { $method = 'get' . ucfirst($param); if (method_exists($this, $method)) { return $this->{$method}(); } } } 3 trả về 1 string tên của class và class User { protected function getUsername() { //.... } public function __get($param) { $method = 'get' . ucfirst($param); if (method_exists($this, $method)) { return $this->{$method}(); } } } 4 trả về 1 mảng tên các method trong object đó.
Một hàm khác cũng hay dùng đấy là class User { protected function getUsername() { //.... } public function __get($param) { $method = 'get' . ucfirst($param); if (method_exists($this, $method)) { return $this->{$method}(); } } } 9:
class User { protected function getUsername() { //.... } public function __get($param) { $method = 'get' . ucfirst($param); if (method_exists($this, $method)) { return $this->{$method}(); } } }Nhìn cũng đủ hiểu chức năng của cái hàm này là gì rồi đúng không?
PHP Reflection Class
Để dễ hiểu hơn ta có 1 số class như sau:
<?php namespace App\Models\Facebook; class UUID { } abstract class Entity { } interface Friendable { } interface Likeable { } interface Postable { } class User extends Entity implements Friendable, Likeable, Postable { public function __construct($name, UUID $uuid) { } public function like(Likebable $entity) { } public function friend(User $user) { } public function post(Post $post) { } } $reflection = new \ReflectionClass(new User('Sơn Tùng Ôm Ti Vi', new UUID(1234)));Get class name
Get full name
echo $reflection->getName(); // App\Models\Facebook\UserGet name
echo $reflection->getShortName(); // UserGet namespace
echo $reflection->getNamespaceName(); // App\Models\FacebookGet parent class
Chúng ta có 1 instance ReflectionClass mới của class cha của User
$parent = $reflection->getParentClass(); echo $parent->getName(); // App\Models\Facebook\EntityGet interfaces
$interfaces = $reflection->getInterfaceNames(); echo "<pre>"; //Đây là cách hay dùng khi mà chưa biết đến Laravel :v var_dump($interfaces); /* array(3) { [0]=> string(28) "App\Models\Facebook\Friendable" [1]=> string(26) "App\Models\Facebook\Likeable" [2]=> string(26) "App\Models\Facebook\Postable" } */Hoặc get 1 mảng các ReflectionClass instances của các interfaces
$interfaces = $reflection->getInterfaces(); echo "<pre>"; var_dump($interfaces); /* array(3) { ["App\Models\Facebook\Friendable"]=> &object(ReflectionClass)#3 (1) { ["name"]=> string(28) "App\Models\Facebook\Friendable" } ["App\Models\Facebook\Likeable"]=> &object(ReflectionClass)#4 (1) { ["name"]=> string(26) "App\Models\Facebook\Likeable" } ["App\Models\Facebook\Postable"]=> &object(ReflectionClass)#5 (1) { ["name"]=> string(26) "App\Models\Facebook\Postable" } } */Get class methods
var_dump(get_class($user)); //App\Models\User var_dump(get_class_method($user)); //Method A //Method B //Method C //Something else 0Get constructor
var_dump(get_class($user)); //App\Models\User var_dump(get_class_method($user)); //Method A //Method B //Method C //Something else 1Chúng ta có thể xem đầu vào của hàm khởi tạo này
var_dump(get_class($user)); //App\Models\User var_dump(get_class_method($user)); //Method A //Method B //Method C //Something else 2Cái <?php namespace App\Models\Facebook; class UUID { } abstract class Entity { } interface Friendable { } interface Likeable { } interface Postable { } class User extends Entity implements Friendable, Likeable, Postable { public function __construct($name, UUID $uuid) { } public function like(Likebable $entity) { } public function friend(User $user) { } public function post(Post $post) { } } $reflection = new \ReflectionClass(new User('Sơn Tùng Ôm Ti Vi', new UUID(1234))); 0 này cũng là 1 mảng các instance <?php namespace App\Models\Facebook; class UUID { } abstract class Entity { } interface Friendable { } interface Likeable { } interface Postable { } class User extends Entity implements Friendable, Likeable, Postable { public function __construct($name, UUID $uuid) { } public function like(Likebable $entity) { } public function friend(User $user) { } public function post(Post $post) { } } $reflection = new \ReflectionClass(new User('Sơn Tùng Ôm Ti Vi', new UUID(1234))); 1, chúng ta lại có thể dùng được:
var_dump(get_class($user)); //App\Models\User var_dump(get_class_method($user)); //Method A //Method B //Method C //Something else 3getDocComment
Như đã nói ở trên, chúng ta có thể get comment của 1 class. Ví dụ ta có 1 class như này:
var_dump(get_class($user)); //App\Models\User var_dump(get_class_method($user)); //Method A //Method B //Method C //Something else 4và kết quả là như thế này
var_dump(get_class($user)); //App\Models\User var_dump(get_class_method($user)); //Method A //Method B //Method C //Something else 5Làm gì với Refection
Giả sử ta có các class sau
var_dump(get_class($user)); //App\Models\User var_dump(get_class_method($user)); //Method A //Method B //Method C //Something else 6Khởi tạo 1 book thì có thể <?php namespace App\Models\Facebook; class UUID { } abstract class Entity { } interface Friendable { } interface Likeable { } interface Postable { } class User extends Entity implements Friendable, Likeable, Postable { public function __construct($name, UUID $uuid) { } public function like(Likebable $entity) { } public function friend(User $user) { } public function post(Post $post) { } } $reflection = new \ReflectionClass(new User('Sơn Tùng Ôm Ti Vi', new UUID(1234))); 2 là 1 string hoặc là 1 instance của <?php namespace App\Models\Facebook; class UUID { } abstract class Entity { } interface Friendable { } interface Likeable { } interface Postable { } class User extends Entity implements Friendable, Likeable, Postable { public function __construct($name, UUID $uuid) { } public function like(Likebable $entity) { } public function friend(User $user) { } public function post(Post $post) { } } $reflection = new \ReflectionClass(new User('Sơn Tùng Ôm Ti Vi', new UUID(1234))); 3
var_dump(get_class($user)); //App\Models\User var_dump(get_class_method($user)); //Method A //Method B //Method C //Something else 7Sửa class Book
var_dump(get_class($user)); //App\Models\User var_dump(get_class_method($user)); //Method A //Method B //Method C //Something else 8Nhìn qua ta thấy ngay là chạy <?php namespace App\Models\Facebook; class UUID { } abstract class Entity { } interface Friendable { } interface Likeable { } interface Postable { } class User extends Entity implements Friendable, Likeable, Postable { public function __construct($name, UUID $uuid) { } public function like(Likebable $entity) { } public function friend(User $user) { } public function post(Post $post) { } } $reflection = new \ReflectionClass(new User('Sơn Tùng Ôm Ti Vi', new UUID(1234))); 4 sẽ bị lỗi và <?php namespace App\Models\Facebook; class UUID { } abstract class Entity { } interface Friendable { } interface Likeable { } interface Postable { } class User extends Entity implements Friendable, Likeable, Postable { public function __construct($name, UUID $uuid) { } public function like(Likebable $entity) { } public function friend(User $user) { } public function post(Post $post) { } } $reflection = new \ReflectionClass(new User('Sơn Tùng Ôm Ti Vi', new UUID(1234))); 5 cho ta kết quả là "Nam Cao". Vào thời điểm runtime $book2, PHP sẽ kiểm tra $author truyền vào cho constructor và tự hiểu $author là 1 instance của class Author và có 1 method là getName(). Đó chính là Refection mà PHP đã dùng để biết được kiểu của biến truyền vào.
Mấy ngôn ngữ mà làm được việc trên gọi là <?php namespace App\Models\Facebook; class UUID { } abstract class Entity { } interface Friendable { } interface Likeable { } interface Postable { } class User extends Entity implements Friendable, Likeable, Postable { public function __construct($name, UUID $uuid) { } public function like(Likebable $entity) { } public function friend(User $user) { } public function post(Post $post) { } } $reflection = new \ReflectionClass(new User('Sơn Tùng Ôm Ti Vi', new UUID(1234))); 6, là ngôn ngữ có thể tự hiểu được object tại thời điểm runtime, không cần tại compile time. PHP, Ruby, Python là <?php namespace App\Models\Facebook; class UUID { } abstract class Entity { } interface Friendable { } interface Likeable { } interface Postable { } class User extends Entity implements Friendable, Likeable, Postable { public function __construct($name, UUID $uuid) { } public function like(Likebable $entity) { } public function friend(User $user) { } public function post(Post $post) { } } $reflection = new \ReflectionClass(new User('Sơn Tùng Ôm Ti Vi', new UUID(1234))); 6. Ngược lại C hay Java là <?php namespace App\Models\Facebook; class UUID { } abstract class Entity { } interface Friendable { } interface Likeable { } interface Postable { } class User extends Entity implements Friendable, Likeable, Postable { public function __construct($name, UUID $uuid) { } public function like(Likebable $entity) { } public function friend(User $user) { } public function post(Post $post) { } } $reflection = new \ReflectionClass(new User('Sơn Tùng Ôm Ti Vi', new UUID(1234))); 8.
Kết luận
- Cảm ơn nhà tài trợ Google search đã đồng hành cùng bài viết này.