Skip to content
Advertisement

PHP encapsulation without class?

Is it possible to encapsulate, a variable or function let say, in PHP without wrapping them in a class? What I was doing is:

JavaScript

My intention is to avoid naming conflict. This routine, although it works, makes me tired. If I cannot do it without class, it is possible not to instantiate a class? and just use the variable or function instantly?

Advertisement

Answer

To use class methods and variables without instantiating, they must be declared static:

JavaScript

With PHP 5.3, you can also use namespaces

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement