Hướng dẫn php reflection call method - phương thức gọi phản chiếu php

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ội dung chính

  • Reflection là gì?
  • Nó dùng để làm gì?
  • Các hàm Reflection thông dụng
  • PHP Reflection Class
  • Get class name
  • Get parent class
  • Get interfaces
  • Get class methods
  • Get constructor
  • getDocComment
  • Làm gì với Refection
  • Kết luận

Nội dung chính

  • Reflection là gì?
  • Nó dùng để làm gì?
  • Các hàm Reflection thông dụng
  • PHP Reflection Class
  • Get class name
  • Get parent class
  • Get interfaces
  • Get class methods
  • Get constructor
  • getDocComment
  • Làm gì với Refection
  • Kết luận

Nội dung chính

  • Reflection là gì?
  • Nó dùng để làm gì?
  • Các hàm Reflection thông dụng
  • PHP Reflection Class
  • Get class name
  • Get parent class
  • Get interfaces
  • Get class methods
  • Get constructor
  • getDocComment
  • Làm gì với Refection
  • Kết luận

Nó dùng để làm gì?

Các hàm Reflection thông dụng

PHP Reflection Class

Get class name

Các hàm Reflection thông dụng

PHP Reflection Class

Get class name

Get parent class

Get interfaces

class User {

    protected function getUsername()
    {
        //....
    }

    public function __get($param)
    {
        $method = 'get' . ucfirst($param);

        if (method_exists($this, $method)) {
            return $this->{$method}();
        }
    }
}

Get class methods

PHP Reflection Class

Get class name



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 parent class

echo $reflection->getName();
// App\Models\Facebook\User

Get interfaces

echo $reflection->getShortName();
// User

Get class methods

echo $reflection->getNamespaceName();
// App\Models\Facebook

Get parent class

Get interfaces

$parent = $reflection->getParentClass();
echo $parent->getName();
// App\Models\Facebook\Entity

Get interfaces

$interfaces = $reflection->getInterfaceNames();

echo "
"; //Đâ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"
}
*/

Get class methods

$interfaces = $reflection->getInterfaces();

echo "
";
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

$methods = $reflection->getMethods();
var_dump($methods);

echo "
";
var_dump($methods);
/*
array(3) {
  [0]=>
  &object(ReflectionMethod)#2 (2) {
    ["name"]=>
    string(4) "like"
    ["class"]=>
    string(22) "App\Models\Facebook\User"
  }
  [1]=>
  &object(ReflectionMethod)#3 (2) {
    ["name"]=>
    string(6) "friend"
    ["class"]=>
    string(22) "App\Models\Facebook\User"
  }
  [2]=>
  &object(ReflectionMethod)#4 (2) {
    ["name"]=>
    string(4) "post"
    ["class"]=>
    string(22) "App\Models\Facebook\User"
  }
}
*/

Get constructor

class User {

    protected function getUsername()
    {
        //....
    }

    public function __get($param)
    {
        $method = 'get' . ucfirst($param);

        if (method_exists($this, $method)) {
            return $this->{$method}();
        }
    }
}
0

getDocComment

class User {

    protected function getUsername()
    {
        //....
    }

    public function __get($param)
    {
        $method = 'get' . ucfirst($param);

        if (method_exists($this, $method)) {
            return $this->{$method}();
        }
    }
}
1

Làm gì với Refection

class User {

    protected function getUsername()
    {
        //....
    }

    public function __get($param)
    {
        $method = 'get' . ucfirst($param);

        if (method_exists($this, $method)) {
            return $this->{$method}();
        }
    }
}
2

getDocComment

Làm gì với Refection

class User {

    protected function getUsername()
    {
        //....
    }

    public function __get($param)
    {
        $method = 'get' . ucfirst($param);

        if (method_exists($this, $method)) {
            return $this->{$method}();
        }
    }
}
3

Kết luận

class User {

    protected function getUsername()
    {
        //....
    }

    public function __get($param)
    {
        $method = 'get' . ucfirst($param);

        if (method_exists($this, $method)) {
            return $this->{$method}();
        }
    }
}
4

Làm gì với Refection

Kết luận

class User {

    protected function getUsername()
    {
        //....
    }

    public function __get($param)
    {
        $method = 'get' . ucfirst($param);

        if (method_exists($this, $method)) {
            return $this->{$method}();
        }
    }
}
5

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.

class User {

    protected function getUsername()
    {
        //....
    }

    public function __get($param)
    {
        $method = 'get' . ucfirst($param);

        if (method_exists($this, $method)) {
            return $this->{$method}();
        }
    }
}
6

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}();
        }
    }
}
8 và
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}();
        }
    }
}
7

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.

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}();
        }
    }
}
8 và


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:

Kết luận

  • 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.