Welcome Guest, Not a member yet? Register   Sign In
Search for a certain function in all helper files?
#11

[eluser]Nummero2[/eluser]
Ok if I code as following:

Code:
$helpers = glob(APPPATH.'helpers/*_helper.php');

$i=0;
foreach ($helpers as $line) {


preg_match('/function([^\(]+)/',$line,$matches_array);

echo var_dump( $matches_array );
$i++;
}

I get the following on the screen:

"array(0) { } array(0) { } array(0) { } array(0) { } array(0) { } array(0) { } array(0) { } array(0) { } array(2) { [0]=> string(20) "functions_helper.php" [1]=> string(12) "s_helper.php" }"

When I leave the ^ out of the "old" statement I get an empty array as before.

The function in the helper files looks like this:

Code:
function example_helper_test( $echo=false )

so example are the various helper files like: example1 example2 and so on the funcion is always _helper_test .
#12

[eluser]sophistry[/eluser]
ok, sorry... there are a few problems still with the regex approach. the variable $line is misleading because it actually has a file name in it and not a line. you have to add

file_get_contents($line)

inside the loop to be able to get the file contents and parse those strings...

BUT........

i'm going to go back to my original post and say that you should just load up all the helpers in the helpers dir (i saw some nice code in your previous thread that seems to work).

then if the function you are looking for exists, function_exists() returns true.

doing all this string parsing with helper files seems counterproductive. i still don't understand why it is necessary.

take a step back from the problem, read the CI manual (there is one in spanish en caso de que necesites)
#13

[eluser]Nummero2[/eluser]
Code:
$this->load->helpers();

foreach ($this as $B) {

if (function_exists('css_helper_test')) {
    echo "function available.<br />\n";
    break;
} else {
    echo "function NOT available.<br />\n";
}

}

Ok, if I search specifically for the function I find it for example (css_helper_test) but now how do I manage to search for my _helper_test context which I actually want.

How do I code that?

Code:
if (function_exists('?????_helper_test')) {

Greetins
#14

[eluser]Nummero2[/eluser]
Now I got the following idea:

Code:
$this->load->helpers();

$files = glob(APPPATH.'helpers/*_helper.php');
foreach($files as $key=>$file)
{
$files[$key] = str_replace('_helper.php','',substr(strrchr($file,'/'),1));

}


foreach ($this as $B) {

if (function_exists('canvas_helper_test')) {
    $this->mymenue->hinweis = "Test: css_helper ";
    $canvas = canvas_helper_test();
    break;
} else {
    echo "IMAP-Funktionen sind nicht verfügbar.<br />\n";
}

}

The first part gives me the helper names (canvas, css and so on) and puts it in an array $files .

Can I use that for my purpose somehow? Like:

Code:
if (function_exists('$files_helper_test')) {

or something similiar?

Frustrating :-(

Greetings
#15

[eluser]Nummero2[/eluser]
wrong? help? Please?
#16

[eluser]xwero[/eluser]
I'm not sure what you are doing with that code snippet. Is it your idea if you call the load->helpers method all helpers will be loaded? And then search for a function that is defined?

If that is the case loading all helpers to check if a function is defined takes up resources that could be used for more useful purposes and it overshoots your goal of loading certain functions from helpers because all functions are defined.

I think you should simplify you problem instead of making it more complex. Write out what your code needs to do and find the easiest solution to code it.
#17

[eluser]Nummero2[/eluser]
yes the problem is there is a test function in all the helpers in the form

css_helper_test
canvas_helper_test
merge_helper_test
and so on

in a foreach loop I want to look for the context _helper_test so the routine is calling the functions automatically. When I load up all the helpers I can call one specific function with function_exists like css_helper_test for example but thats not what I want.
#18

[eluser]xwero[/eluser]
It's not smart to have the same named functions in different helpers as all the functions are added to the global namespace.
The easiest way to circumvent this is to prefix same name functions in different helpers, for example css_test, canvas_test.

If you load the helper all functions in the helper are loaded because the helper file is included. You can't select specified functions after you load a helper because they are all added already.

You should really move from the current thoughtpad to find other ways to solve your problem.




Theme © iAndrew 2016 - Forum software by © MyBB