1

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

//Include the file containing the class which contains the variable or function include('SomePage.php'); //Instantiate the class from "SomePage.php" $NewObject = new SomeClassFromSomePage(); //Use the function or variable echo $NewObject->SomeFuncFromSomeClass(); echo $NewObject->SomeVarFromSomeClass; 

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?

3
  • You probably want to use a factory method (or companion procedure) for that. If the creation of the variable depends on previous method invocation, then you also need fluent chaining. Commented Aug 10, 2011 at 1:55
  • @domanokz Can you describe what you mean by "avoid naming conflict"? Commented Aug 10, 2011 at 1:57
  • @Phil I might have the same $x in main page and the included page. Commented Aug 10, 2011 at 2:00

3 Answers 3

3

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

class My_Class { public static $var = 123; public static function getVar() { return self::var; } } // Call as: My_Class::getVar(); // or access the variable directly: My_Class::$var; 

With PHP 5.3, you can also use namespaces

namespace YourNamespace; function yourFunction() { // do something... } // While in the same namespace, call as yourFunction(); // From a different namespace, call as YourNamespace\yourFunction(); 
Sign up to request clarification or add additional context in comments.

5 Comments

In this case how do I use the $var? or the $getVar?
@domanokz See the additions I made, probably while you were typing your comment
Thanks, but the double colon is new to me, must I use :: instead of ->?
@domanokz Static methods and properties are accessed with the double colon scope operator, while methods and properties of an instantiated class are accessed with the ->
2

PHP Namespaces were made to archive the exact same goal:

<?php // foo.php namespace Foo; function bar() {} class baz { static $qux; } ?> 

When using call namespaced functions like this:

<?php //bar.php include 'foo.php'; Foo\bar(); Foo\baz::$qux = 1; ?> 

2 Comments

I think @domanokz wants to avoid variable name conflicts. Namespaces do not solve this, see stackoverflow.com/questions/5287315/…
@Phil static class variable is the only option here, as I see
0

This is a way to encapsulate without Class

<?php (function (){ $xyz = 'XYZ'; })(); echo $xyz; // warning: undefined 

Encapsulation Alternative

With this method you can minimize unintentional using array key(uses it instead of variables). Can also use value stored in array anywhere after assigning. Shorter array key area length with variable in keys, inside encapsulation function; outside encapsulation function, variables can be used in keys but otherwise long discriptive keys. Nested encapsulation can also be used.

Example

<?php define('APP', 'woi49f25gtx'); (function () { $pre = 'functions__math__'; // "functions" is main category, "math" is sub. $GLOBALS[APP][$pre . 'allowedNumbers'] = [3,5,6]; $GLOBALS[APP][$pre . 'square'] = function ($num) { return $num * $num; }; $GLOBALS[APP][$pre . 'myMathFunction'] = function ($num) use ($pre) { if(in_array($num,$GLOBALS[APP][$pre . 'allowedNumbers'])) return 'not allowed'; return $GLOBALS[APP][$pre . 'square']($num); }; })(); echo $GLOBALS[APP]['functions__math__myMathFunction'](4); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.