Beware: Kittens at risk

davecoventry's picture

He has: 112 posts

Joined: Jun 2009

JB, at the risk of losing kittens to the wrath of the Almighty, I need to enquire about namsespaces in PHP.

I have an array that I want to refer to.

I have 3 or 4 different blocks and nodes that refer to the same array.

Years ago, when I programed javascript, I would have a file which I would call davesvars.js which would contain something like:

var foo=new Array('stacks','of different','values in an','array');

Then I would use and the array would be accessible to my scripts.

What would be the best way of doing this? Could I create an node programatically in which to keep the values? What would you advise?

Greg K's picture

He has: 2,145 posts

Joined: Nov 2003

You could have a file that is included:

davevars.php:
$GLOBALS['foo'] = array('Stacks','of different','values  an','array);

Then in your other code , do a
require_once('path/to/davevars.php');

I use the $GLOBALS['foo'] instead of just $foo, so that the include can be called from anywhere (even inside a function or class), and it will still be accessible everywhere.

While outside of a classes/functions you can directly access it as $foo, I still prefer to do $GLOBAL['foo'] so that later going back I know I'm accessing something that would be used as a "global" value.

-Greg

davecoventry's picture

He has: 112 posts

Joined: Jun 2009

Right.

that's very useful.

Thanks Greg.

JeevesBond's picture

He has: 3,956 posts

Joined: Jun 2002

I would use variable_set() and variable_get().

Then, assuming you're writing a module, put a call to variable_del() into the module's hook_uninstall().

You could also use globals, just make sure they're appropriate for what you're trying to do. Globals can be evil if used improperly (naming collisions, functions unexpectedly changing their values and such). Smiling

a Padded Cell our articles site!

davecoventry's picture

He has: 112 posts

Joined: Jun 2009

Still on namespaces; I've got a procedure that I use to push the results of my SQL query onto an array in a particular format.

I use this same array in a number of instances.

Is there a way I can check to see if the function is already defined?

At the moment I'm giving the function a slightly different name and defining it at the head of each instance. Obviously, this is not best practice...

JeevesBond's picture

He has: 3,956 posts

Joined: Jun 2002

At the moment I'm giving the function a slightly different name and defining it at the head of each instance.

Am not sure what the word 'instance' means here. That does sound quite ghaslty though.

In PHP determining if an function already exists is done by calling function_exists('function_name'). I don't believe it's a good idea to declare functions in multiple places though. Smiling

a Padded Cell our articles site!

davecoventry's picture

He has: 112 posts

Joined: Jun 2009

JeevesBond wrote:

In PHP determining if an function already exists is done by calling function_exists('function_name'). I don't believe it's a good idea to declare functions in multiple places though. :)

Indeed! Horrible Hack.

One of my blocks already used the function and when the page which had my PHP on it was called it tried to define the same function which generated an error message about being unable to redefine the function.

The quick fix was to change the name slightly, but, as you say, it's not good and I need to rewrite it so that it only defines the function if it hasn't been previously defined.

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.