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?

kazinix
  • 28,987
  • 33
  • 107
  • 157
  • 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. – mario Aug 10 '11 at 01:55
  • @domanokz Can you describe what you mean by "avoid naming conflict"? – Phil Aug 10 '11 at 01:57
  • @Phil I might have the same `$x` in main page and the included page. – kazinix Aug 10 '11 at 02:00

3 Answers3

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();
Michael Berkowski
  • 267,341
  • 46
  • 444
  • 390
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;
?>
sanmai
  • 29,083
  • 12
  • 64
  • 76
  • I think @domanokz wants to avoid variable name conflicts. Namespaces do not solve this, see http://stackoverflow.com/questions/5287315/can-php-namespaces-contain-variables – Phil Aug 10 '11 at 02:07
  • @Phil static class variable is the only option here, as I see – sanmai Aug 10 '11 at 02:25
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);
proseosoc
  • 1,168
  • 14
  • 23