Welcome Guest, Not a member yet? Register   Sign In
Get all Functions Names of All CONTROLLERS
#1
Exclamation 

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
Reply
#2

get_class_methods() should do the trick.
Reply
#3

@kilishan

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

(This post was last modified: 02-03-2016, 12:06 PM by ivantcholakov.)

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.
Reply
#5

(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.
Reply
#6

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-co...t.php#L130
Reply
#7

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>'
Reply




Theme © iAndrew 2016 - Forum software by © MyBB