CodeIgniter Forums
Get all Functions Names of All CONTROLLERS - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Get all Functions Names of All CONTROLLERS (/showthread.php?tid=64277)



Get all Functions Names of All CONTROLLERS - azammaqsood - 02-03-2016

Hello I want to create a new controller in controllers folder
and want to get all controllers names in controllers name and then all function names in each controller
but i need to write code in only one controller

so far i succeed in getting all controller names but i don't know how to get each function name in each controller 
plz help me


RE: Get all Functions Names of All CONTROLLERS - kilishan - 02-03-2016

get_class_methods() should do the trick.


RE: Get all Functions Names of All CONTROLLERS - ivantcholakov - 02-03-2016

@kilishan

But PHP will parse all the controller files then. What do you think about scanning the sources with a regular expression?


RE: Get all Functions Names of All CONTROLLERS - ivantcholakov - 02-03-2016

Another possible way is using http://www.php.net/manual/en/function.token-get-all.php
but it requires writing code for detection a sequence of tokens, for example: T_PUBLIC T_WHITESPACE T_FUNCTION T_WHITESPACE T_STRING

Edit: Trying how it works:

Code:
$source = <<<EOT
<?php

class MyClass
{
    public function my_nethod_1()
    {
    }

    public function my_nethod_2()
    {
    }
}

EOT;

$tokens = token_get_all($source);

foreach ($tokens as $token) {

    if (is_array($token)) {
        print_r($token); echo '<br />';
    }
}

I have never used the tokenizer, it requires a little bit practicing, but it seems to me a good choice for this task.


RE: Get all Functions Names of All CONTROLLERS - kilishan - 02-03-2016

(02-03-2016, 11:14 AM)ivantcholakov Wrote: @kilishan

But PHP will parse all the controller files then. What do you think about scanning the sources with a regular expression?

That's true, it will parse them all, but I'm pretty sure that it, being a native method written in C, is likely to have better performance than a regular expression. While I think either way could work, I think the performance difference would be negligible between the two. And, since I don't know what the purpose is, I would personally go with the easier solution. Then, if performance was found to be an issue down the road, I would simply cache the results.


RE: Get all Functions Names of All CONTROLLERS - ivantcholakov - 02-06-2016

There is a Zend package capable of doing this, but it requires time for learning, indeed. It uses a tokenizer. Namespace for ZF2: Zend\Code\Scanner

Here is a test that gives an idea on the subject:
https://github.com/zendframework/zend-code/blob/master/test/Scanner/ClassScannerTest.php#L130


RE: Get all Functions Names of All CONTROLLERS - skunkbad - 02-06-2016

Maybe:


PHP Code:
$this->load->helper('file');

$controllers get_filenamesAPPPATH 'controllers/' ); 

foreach( 
$controllers as $k => $v )
{
    if( strpos$v'.php' ) === FALSE)
    {
        unset( $controllers[$k] );
    }
}

echo 
'<ul>';

foreach( 
$controllers as $controller )
{
    echo '<li>' $controller '<ul>';

    include_once APPPATH 'controllers/' $controller;

    $methods get_class_methodsstr_replace'.php'''$controller ) );

    foreach( $methods as $method )
    {
        echo '<li>' $method '</li>';
    }

    echo '</ul></li>';
}

echo 
'</ul>'