File private variables in PHP

Posted by kayahr on Stack Overflow See other posts from Stack Overflow or by kayahr
Published on 2010-04-10T13:58:05Z Indexed on 2010/04/10 14:03 UTC
Read the original article Hit count: 280

Filed under:
|

Is it possible to define private variables in a PHP script so these variables are only visible in this single PHP script and nowhere else? I want to have an include file which does something without polluting the global namespace. It must work with PHP 5.2 so PHP namespaces are not an option. And no OOP is used here so I'm not searching for private class members. I'm searching for "somewhat-global" variables which are global in the current script but nowhere else.

In C I could do it with the static keyword but is there something similar in PHP?

Here is a short example of a "common.php" script:

$dir = dirname(__FILE__);
set_include_path($dir . PATH_SEPARATOR . get_include_path());
// Do more stuff with the $dir variable

When I include this file in some script then the $dir variable is visible in all other scripts as well and I don't want that. So how can I prevent this?

© Stack Overflow or respective owner

Related posts about php

Related posts about php5