Cara menggunakan php reflection get methods

PHP | ReflectionClass getMethods() Function

Show

Improve Article

Save Article

Like Article

  • Last Updated : 06 Jan, 2023

  • Read
  • Discuss
  • Courses
  • Practice
  • Video
  • Improve Article

    Save Article

    The ReflectionClass::getMethods() function is an inbuilt function in PHP which is used to return an array of specified methods.
    Syntax: 

    array ReflectionClass::getMethods( int $filter )

    Parameters: This function accepts a single parameter filter which is used to remove some of the methods.
    Return Value: This function returns an array of specified methods.
    Below programs illustrate the ReflectionClass::getMethods() function in PHP:
    Program 1:  

    php




    <?php

     

    // Initialising a user-defined Class

    class Departments {

        public function

    &lt;?php
    
    // Initialising a user-defined Class
    class Departments {
        public function CSE() { }
        final protected function ECE() { }
        private static function EE() { }
        static function IT() { }
        private function Mechanical() { }
    }
    
    // Using ReflectionClass() over the
    // user-defined class Departments
    $class = new ReflectionClass('Departments');
    
    // Calling the getMethods() function
    $methods = $class-&gt;getMethods();
    
    // Getting an array of specified methods
    var_dump($methods);
    ?&gt;
    0

        

    &lt;?php
    
    // Initialising a user-defined Class
    class Departments {
        public function CSE() { }
        final protected function ECE() { }
        private static function EE() { }
        static function IT() { }
        private function Mechanical() { }
    }
    
    // Using ReflectionClass() over the
    // user-defined class Departments
    $class = new ReflectionClass('Departments');
    
    // Calling the getMethods() function
    $methods = $class-&gt;getMethods();
    
    // Getting an array of specified methods
    var_dump($methods);
    ?&gt;
    2
    &lt;?php
    
    // Initialising a user-defined Class
    class Departments {
        public function CSE() { }
        final protected function ECE() { }
        private static function EE() { }
        static function IT() { }
        private function Mechanical() { }
    }
    
    // Using ReflectionClass() over the
    // user-defined class Departments
    $class = new ReflectionClass('Departments');
    
    // Calling the getMethods() function
    $methods = $class-&gt;getMethods();
    
    // Getting an array of specified methods
    var_dump($methods);
    ?&gt;
    3 function
    &lt;?php
    
    // Initialising a user-defined Class
    class Departments {
        public function CSE() { }
        final protected function ECE() { }
        private static function EE() { }
        static function IT() { }
        private function Mechanical() { }
    }
    
    // Using ReflectionClass() over the
    // user-defined class Departments
    $class = new ReflectionClass('Departments');
    
    // Calling the getMethods() function
    $methods = $class-&gt;getMethods();
    
    // Getting an array of specified methods
    var_dump($methods);
    ?&gt;
    5

        

    &lt;?php
    
    // Initialising a user-defined Class
    class Departments {
        public function CSE() { }
        final protected function ECE() { }
        private static function EE() { }
        static function IT() { }
        private function Mechanical() { }
    }
    
    // Using ReflectionClass() over the
    // user-defined class Departments
    $class = new ReflectionClass('Departments');
    
    // Calling the getMethods() function
    $methods = $class-&gt;getMethods();
    
    // Getting an array of specified methods
    var_dump($methods);
    ?&gt;
    7
    &lt;?php
    
    // Initialising a user-defined Class
    class Departments {
        public function CSE() { }
        final protected function ECE() { }
        private static function EE() { }
        static function IT() { }
        private function Mechanical() { }
    }
    
    // Using ReflectionClass() over the
    // user-defined class Departments
    $class = new ReflectionClass('Departments');
    
    // Calling the getMethods() function
    $methods = $class-&gt;getMethods();
    
    // Getting an array of specified methods
    var_dump($methods);
    ?&gt;
    8 function
    array(2) {
      [0]=>
      object(ReflectionMethod)#2 (2) {
        ["name"]=>
        string(2) "EE"
        ["class"]=>
        string(11) "Departments"
      }
      [1]=>
      object(ReflectionMethod)#3 (2) {
        ["name"]=>
        string(2) "IT"
        ["class"]=>
        string(11) "Departments"
      }
    }
    0

        

    &lt;?php
    
    // Initialising a user-defined Class
    class Departments {
        public function CSE() { }
        final protected function ECE() { }
        private static function EE() { }
        static function IT() { }
        private function Mechanical() { }
    }
    
    // Using ReflectionClass() over the
    // user-defined class Departments
    $class = new ReflectionClass('Departments');
    
    // Calling the getMethods() function
    $methods = $class-&gt;getMethods();
    
    // Getting an array of specified methods
    var_dump($methods);
    ?&gt;
    8 function
    array(2) {
      [0]=>
      object(ReflectionMethod)#2 (2) {
        ["name"]=>
        string(2) "EE"
        ["class"]=>
        string(11) "Departments"
      }
      [1]=>
      object(ReflectionMethod)#3 (2) {
        ["name"]=>
        string(2) "IT"
        ["class"]=>
        string(11) "Departments"
      }
    }
    4

        

    &lt;?php
    
    // Initialising a user-defined Class
    class Departments {
        public function CSE() { }
        final protected function ECE() { }
        private static function EE() { }
        static function IT() { }
        private function Mechanical() { }
    }
    
    // Using ReflectionClass() over the
    // user-defined class Departments
    $class = new ReflectionClass('Departments');
    
    // Calling the getMethods() function
    $methods = $class-&gt;getMethods();
    
    // Getting an array of specified methods
    var_dump($methods);
    ?&gt;
    7 function
    array(2) {
      [0]=>
      object(ReflectionMethod)#2 (2) {
        ["name"]=>
        string(2) "EE"
        ["class"]=>
        string(11) "Departments"
      }
      [1]=>
      object(ReflectionMethod)#3 (2) {
        ["name"]=>
        string(2) "IT"
        ["class"]=>
        string(11) "Departments"
      }
    }
    8

    array(2) {
      [0]=>
      object(ReflectionMethod)#2 (2) {
        ["name"]=>
        string(2) "EE"
        ["class"]=>
        string(11) "Departments"
      }
      [1]=>
      object(ReflectionMethod)#3 (2) {
        ["name"]=>
        string(2) "IT"
        ["class"]=>
        string(11) "Departments"
      }
    }
    9

     

    <?php0

    <?php1

    <?php2 <?php3<?php4 <?php5<?php6<?php7

     

    <?php8

    <?php9 <?php3<?php2// Initialising a user-defined Class2

     

    // Initialising a user-defined Class3

    // Initialising a user-defined Class4<?php9<?php7

    // Initialising a user-defined Class7

    Output

    &lt;?php
    
    // Initialising a user-defined Class
    class Departments {
        public function CSE() { }
        final protected function ECE() { }
        private static function EE() { }
        static function IT() { }
        private function Mechanical() { }
    }
    
    // Using ReflectionClass() over the
    // user-defined class Departments
    $class = new ReflectionClass('Departments');
    
    // Calling the getMethods() function
    $methods = $class-&gt;getMethods();
    
    // Getting an array of specified methods
    var_dump($methods);
    ?&gt;

    Program 2: 

    php




    <?php

     

    // Initialising a user-defined Class

    class Departments {

        public function

    &lt;?php
    
    // Initialising a user-defined Class
    class Departments {
        public function CSE() { }
        final protected function ECE() { }
        private static function EE() { }
        static function IT() { }
        private function Mechanical() { }
    }
    
    // Using ReflectionClass() over the
    // user-defined class Departments
    $class = new ReflectionClass('Departments');
    
    // Calling the getMethods() function
    $methods = $class-&gt;getMethods();
    
    // Getting an array of specified methods
    var_dump($methods);
    ?&gt;
    0

        

    &lt;?php
    
    // Initialising a user-defined Class
    class Departments {
        public function CSE() { }
        final protected function ECE() { }
        private static function EE() { }
        static function IT() { }
        private function Mechanical() { }
    }
    
    // Using ReflectionClass() over the
    // user-defined class Departments
    $class = new ReflectionClass('Departments');
    
    // Calling the getMethods() function
    $methods = $class-&gt;getMethods();
    
    // Getting an array of specified methods
    var_dump($methods);
    ?&gt;
    2
    &lt;?php
    
    // Initialising a user-defined Class
    class Departments {
        public function CSE() { }
        final protected function ECE() { }
        private static function EE() { }
        static function IT() { }
        private function Mechanical() { }
    }
    
    // Using ReflectionClass() over the
    // user-defined class Departments
    $class = new ReflectionClass('Departments');
    
    // Calling the getMethods() function
    $methods = $class-&gt;getMethods();
    
    // Getting an array of specified methods
    var_dump($methods);
    ?&gt;
    3 function
    &lt;?php
    
    // Initialising a user-defined Class
    class Departments {
        public function CSE() { }
        final protected function ECE() { }
        private static function EE() { }
        static function IT() { }
        private function Mechanical() { }
    }
    
    // Using ReflectionClass() over the
    // user-defined class Departments
    $class = new ReflectionClass('Departments');
    
    // Calling the getMethods() function
    $methods = $class-&gt;getMethods();
    
    // Getting an array of specified methods
    var_dump($methods);
    ?&gt;
    5

        

    &lt;?php
    
    // Initialising a user-defined Class
    class Departments {
        public function CSE() { }
        final protected function ECE() { }
        private static function EE() { }
        static function IT() { }
        private function Mechanical() { }
    }
    
    // Using ReflectionClass() over the
    // user-defined class Departments
    $class = new ReflectionClass('Departments');
    
    // Calling the getMethods() function
    $methods = $class-&gt;getMethods();
    
    // Getting an array of specified methods
    var_dump($methods);
    ?&gt;
    7
    &lt;?php
    
    // Initialising a user-defined Class
    class Departments {
        public function CSE() { }
        final protected function ECE() { }
        private static function EE() { }
        static function IT() { }
        private function Mechanical() { }
    }
    
    // Using ReflectionClass() over the
    // user-defined class Departments
    $class = new ReflectionClass('Departments');
    
    // Calling the getMethods() function
    $methods = $class-&gt;getMethods();
    
    // Getting an array of specified methods
    var_dump($methods);
    ?&gt;
    8 function
    array(2) {
      [0]=>
      object(ReflectionMethod)#2 (2) {
        ["name"]=>
        string(2) "EE"
        ["class"]=>
        string(11) "Departments"
      }
      [1]=>
      object(ReflectionMethod)#3 (2) {
        ["name"]=>
        string(2) "IT"
        ["class"]=>
        string(11) "Departments"
      }
    }
    0

        

    &lt;?php
    
    // Initialising a user-defined Class
    class Departments {
        public function CSE() { }
        final protected function ECE() { }
        private static function EE() { }
        static function IT() { }
        private function Mechanical() { }
    }
    
    // Using ReflectionClass() over the
    // user-defined class Departments
    $class = new ReflectionClass('Departments');
    
    // Calling the getMethods() function
    $methods = $class-&gt;getMethods();
    
    // Getting an array of specified methods
    var_dump($methods);
    ?&gt;
    8 function
    array(2) {
      [0]=>
      object(ReflectionMethod)#2 (2) {
        ["name"]=>
        string(2) "EE"
        ["class"]=>
        string(11) "Departments"
      }
      [1]=>
      object(ReflectionMethod)#3 (2) {
        ["name"]=>
        string(2) "IT"
        ["class"]=>
        string(11) "Departments"
      }
    }
    4

        

    &lt;?php
    
    // Initialising a user-defined Class
    class Departments {
        public function CSE() { }
        final protected function ECE() { }
        private static function EE() { }
        static function IT() { }
        private function Mechanical() { }
    }
    
    // Using ReflectionClass() over the
    // user-defined class Departments
    $class = new ReflectionClass('Departments');
    
    // Calling the getMethods() function
    $methods = $class-&gt;getMethods();
    
    // Getting an array of specified methods
    var_dump($methods);
    ?&gt;
    7 function
    array(2) {
      [0]=>
      object(ReflectionMethod)#2 (2) {
        ["name"]=>
        string(2) "EE"
        ["class"]=>
        string(11) "Departments"
      }
      [1]=>
      object(ReflectionMethod)#3 (2) {
        ["name"]=>
        string(2) "IT"
        ["class"]=>
        string(11) "Departments"
      }
    }
    8

    array(2) {
      [0]=>
      object(ReflectionMethod)#2 (2) {
        ["name"]=>
        string(2) "EE"
        ["class"]=>
        string(11) "Departments"
      }
      [1]=>
      object(ReflectionMethod)#3 (2) {
        ["name"]=>
        string(2) "IT"
        ["class"]=>
        string(11) "Departments"
      }
    }
    9

     

    <?php0

    <?php1

    <?php2 <?php3<?php4 <?php5<?php6<?php7

     

    <?php8

    <?php9 <?php3<?php2public7

     

    // Initialising a user-defined Class3

    // Initialising a user-defined Class4<?php9<?php7

    // Initialising a user-defined Class7

    Output: 

    array(2) {
      [0]=>
      object(ReflectionMethod)#2 (2) {
        ["name"]=>
        string(2) "EE"
        ["class"]=>
        string(11) "Departments"
      }
      [1]=>
      object(ReflectionMethod)#3 (2) {
        ["name"]=>
        string(2) "IT"
        ["class"]=>
        string(11) "Departments"
      }
    }

     

    Reference: https://www.php.net/manual/en/reflectionclass.getmethods.php


    My Personal Notes arrow_drop_up

    Save

    Please Login to comment...