views:

115

answers:

7

I have a function that needs to include a file, however this functions is used in a place from 200 to 300 times, this is obviously causing efficiency issues, is there a way to optimize this inside the function? (i know there are many ways in which i can fix this but it will cause too much impact in the whole application)

I will just put a little example, this is not the whole function.

function getString(arrayName, strValue){
    inclue('stringArrays.php');
    return $$arrayName[strValue];
}

I tried using include_once, but that doesn't do the job either.

Thanks in advance.

A: 

You could always add another parameter, perhaps a boolean, to tell the function whether or not to include it.

function getString(arrayName, strValue, includeFile)
{ 
    if (includeFile)
    {
        inclue('stringArrays.php');
    }

    return $$arrayName[strValue]; 
}
Randy
Everytime i call the function i need to include the file, otherwise i can't get any values from it.
Kusanagi2k
A: 

You can try globalizing what's in stringArrays.php so you can check to see if that global variable is already set before including the file. Hard to tell without seeing what structure is in stringArrays.php.

methodin
Yeah that could work if it was done at the beggining of the development, however at this stage is kinda annoying.
Kusanagi2k
Oops didn't mean to submit it. In stringArrays there are just different arrays with many key=>Value, however there's no big array containing everything, every array is different.$values_1=array('test' => 'This is a Test');Right now it has about 200 arrays.
Kusanagi2k
You can change the stringArrays.php to $GLOBALS['values_1'] = array(... thus insuring when you include it from the function it will be set globally and can be read from all other calls of that function. You'd have to access it via: return $GLOBALS[$arrayName][$strValue]; but you can then easily check if(!isset($GLOBALS['values_1'])) inclue('stringArrays.php');
methodin
A: 

If your function does nothing more than include a file you should be first evaluating whether that function should be called in the first place or make the function determine if an include is required. Basically don't blindly include a file if you truly don't need it included. include_once will incur a performance hit.

Veign
No, it not only includes a file, like i said that was just a little example from the whole function, i just typed the basic idea.
Kusanagi2k
A: 

Install APC, eAccelerator, XCache or any other code accelerator so PHP doesn't need to retrieve the include file from disk every time it's called. Code accelerators save the file in shared memory. That will improve performance significantly.

stillstanding
A: 

Is there anything preventing you from wrapping your current "bunch" of arrays in an array, then passing that wrapper array into the function by reference? You can then do a single require/include outside of the function. Alternatively, you can wrap both the set of arrays and the function inside an object, again bringing you down to a single require/include.

Stan Rogers
We'll have to transform arrays from $arrayName to $bigArray['arrayName'] yes it's a solution, but it's too much work, thanks for the help.
Kusanagi2k
A: 

If stringArrays.php is simply a collection of arrays, what about creating a stringHandler singleton that includes stringArrays.php within the constructor and maps the each array to a class property, then a simple method to get whichever you want from that class. Then your getString() function simply references a getter method in the stringHandler.

stringArrays.php

<?php

$abc = array('def' => 'Hello',
             'ghi' => ' '
            );

$jkl = array('mno' => 'World',
             'pqr' => '.'
            );

?>

stringHandler.php

<?php

class stringHandler
{
    private static $instance;

    private function __construct()
    {
        include('stringArrays.php');
        foreach(get_defined_vars() as $key => $val) {
            $this->{$key} = $val;
        }
    }

    public static function singleton()
    {
        if (!isset(self::$instance)) {
            $c = __CLASS__;
            self::$instance = new $c;
        }

        return self::$instance;
    }

    public function getStringFromArray($arrayName, $strValue)
    {
        return $this->{$arrayName}[$strValue];
    }

}

function getString($arrayName, $strValue){
    return stringHandler::singleton()->getStringFromArray($arrayName, $strValue);
}


echo getString('abc','def');
echo getString('abc','ghi');
echo getString('jkl','mno');

?>

Kludgy, but shouldn't be a big performance overhead.

Mark Baker
Yeah i agree with you, this is a better approach specially for an objec oriented application, however, we didn't do it and now it's a mess, in future applications we'll use this, right now sadly we can't but thanks for your help
Kusanagi2k
+1  A: 

You could use a static variable in the function to hold your values:

function getString($arrayName, $strValue){
static $string_arrays = array();
if (empty($string_arrays)) {
    include('stringArrays.php');
    $string_arrays = array_diff_key(get_defined_vars(), array(
        'string_arrays' => true,
        'arrayName' => true,
        'strValue' => true,
    ));
}
return $string_arrays[$arrayName][$strValue];

}

Should only include the file once.

Matt Barry