I would like to know if there is a way to access a variable defined in an other file from a class in PHP.
Example :
file_01.php
<?php
$a = 42;
?>
file_02.php
<?php
require_once('file_01.php');
class mnyClass
{
private $myVar;
function __construct($var = $a)
{
$this->myVar = $var;
}
function getVar()
{
return $this->var;
}
function setVar($var)
{
$this->myVar = $var;
}
}
?>
Obviously, my class is more complicated. I have chosen this example for a better comprehension of what I try to do ;)
Thank you in advance.
You cannot do this:
function __construct($var = $a)
{
$this->myVar = $var;
}
What you can do is pass it:
<?php
require_once('file_01.php');
$mnyClass = new mnyClass($a);// the torch has been passed!
class mnyClass
{
private $myVar;
function __construct($var = null)
{
$this->myVar = $var;
}
function getVar()
{
return $this->var;
}
function setVar($var)
{
$this->myVar = $var;
}
}
?>
OR you can do this (it is not advisable):
function __construct($var = null)
{
if($var === null) $var = $GLOBALS['a']; //use global $a variable
$this->myVar = $var;
}
You could access the variable via GLOBALS:
http://php.net/manual/en/language.variables.scope.php
EDIT: a little more detail-
function __construct() {
$this->myVar = $GLOBALS['a'];
}
It sounds like you're setting up some application defaults. It might make sense to define these as constants:
file_01.php:
define('DEFAULT_VALUE_FOR_A', 42);
file_02.php
class myClass
{
function __construct($var = DEFAULT_VALUE_FOR_A) {
}
}
Finally, I use this method :
<?php
require_once('file_01.php');
class myClass {
private $myVar;
function __construct($var = NULL)
{
global $a;
if($var == NULL)
$this->myVar = $a;
else
$this->myVar = $var;
}
}
?>
I declare my variable $a as global in the constructor, set the default value of my $var to NULL and check if the constructor was called with parameter(s) ($var == NULL).
Related
What do this reference symbol (&) do in &methodName() method?
Is it necessary?
Is this called a reference by method?
class TestClass
{
private $value;
public function __construct()
{
$this->value = 5;
}
public function &methodName(){
return $this->value;
}
}
echo (new TestClass())->methodName(); //Outputs 5;
It means that the method returns a reference. If you then assign that to a reference variable outside the method, updating the variable will update the property.
class TestClass
{
private $value;
public function __construct()
{
$this->value = 5;
}
public function &refMethod(){
return $this->value;
}
public function valueMethod() {
return $this->value;
}
public function printValue() {
echo $this->value . "<br>";
}
}
$c = new TestClass();
$var1 = &$c->valueMethod();
$var1 = 10;
$c->printValue(); // prints 5
$var = &$c->refMethod();
$var = 20;
$c->printValue(); // prints 20
Here's a little mock-up to describe my predicament:
<?php
$var = "Before";
function getVar(){
global $var;
return $var;
}
$array = Array(
"variable" => "Var = " . getVar()
);
$var = "After";
echo $array['variable'];
?>
That code would echo 'Before', I'm aiming for it to echo 'after'. I realize that this is how PHP is supposed to work however it's crucial for the array to execute getVar() only when it's called.
How would I go about doing this?
You can not do this since array declaration will initialize it - so you're mixing function calling at array's 'usage' and at it's definition. There's no 'usage': array is already defined to that moment.
However, an answer could be using ArrayAccess, like this:
class XArray implements ArrayAccess
{
private $storage = [];
public function __construct()
{
$this->storage = func_get_args();
}
public function offsetSet($offset, $value)
{
if(is_null($offset))
{
$this->storage[] = $value;
}
else
{
$this->storage[$offset] = $value;
}
}
public function offsetExists($offset)
{
return isset($this->storage[$offset]);
}
public function offsetUnset($offset)
{
unset($this->storage[$offset]);
}
public function offsetGet($offset)
{
if(!isset($this->storage[$offset]))
{
return null;
}
return is_callable($this->storage[$offset])?
call_user_func($this->storage[$offset]):
$this->storage[$offset];
}
}
function getVar()
{
global $var;
return $var;
}
$var = 'Before Init';
$array = new XArray('foo', 'getVar', 'bar');
$var = 'After Init';
var_dump($array[1]);//'After Init'
-i.e. try to call data, which is inside element, when actual get happened. You may want to have different constructor (for associative arrays) - but the general idea was shown.
Editing my answer after the question was edited.
No, what you are trying to achieve isn't possible because when you call the function it returns and it's done at that point. But you could achieve something similar with object oriented coding. I'll create something for you, please wait.
<?php
class Foo {
public function __toString() {
global $var;
return "Var = {$var}";
}
}
$var = "Before";
$array = array( "variable" => new Foo() );
$var = "After";
echo $array['variable'];
?>
PS: Sorry for the late answer, but there was a blackout in Salzburg. :(
It occurred to me that you could also use anonymous functions and invoke/execute those
Proof of concept:
$var = "Before";
function getVar(){
global $var;
return $var;
}
$array = Array(
"variable" => create_function(null, "return 'Var = ' . getVar();")
);
$var = "After";
echo $array['variable']();
returns
Var = After
Is any way to create class static var inside method?
something like this..
class foo {
public function bind($name, $value) {
self::$name = $value;
}
};
or is there other solution to bind variables to class and later use it without long and ugly syntax "$this->"
I'm not sure I understand the question. But if you'd like to attach variables at runtime, you could do this:
abstract class RuntimeVariableBinder
{
protected $__dict__ = array();
protected function __get($name) {
if (isset($this->__dict__[$name])) {
return $this->__dict__[$name];
} else {
return null;
}
}
protected function __set($name, $value) {
$this->__dict__[$name] = $value;
}
}
class Foo
extends RuntimeVariableBinder
{
// Explicitly allow calling code to get/set variables
public function __get($name) {
return parent::__get($name);
}
public function __set($name, $value) {
parent::__set($name, $value);
}
}
$foo = new Foo();
$foo->bar = "Hello, world!";
echo $foo->bar; // Prints "Hello, world!"
http://codepad.org/H9bz2uVp
Using self would result in a fatal error, as the property is undeclared. You would have to use $this which would then be accessible as a public variable:
<?php
class foo {
public function bind($name, $value) {
$this->$name = $value;
}
}
$foo = new Foo;
$foo->bind('bar','Hello World');
echo '<pre>';
print_r($foo);
echo $foo->bar;
echo '</pre>';?>
i have something like this:
class foo
{
//code
}
$var = new foo();
$var->newVariable = 1; // create foo->newVariable
$var->otherVariable = "hello, im a variable"; //create foo->otherVariable
i can get in class foo a list of all variables defined outside by user (newVariable, otherVariable,etc)? Like this:
class foo
{
public function getUserDefined()
{
// code
}
}
$var = new foo();
$var->newVariable = 1; // create foo->newVariable
$var->otherVariable = "hello, im a variable"; //create foo->otherVariable
var_dump($var->getUserDefined()); // returns array ("newVariable","otherVariable");
Thanks!.
Yes, using get_object_vars() and get_class_vars():
class A {
var $hello = 'world';
}
$a = new A();
$a->another = 'variable';
echo var_dump(get_object_vars($a));
echo '<hr />';
// Then, you can strip off default properties using get_class_vars('A');
$b = get_object_vars($a);
$c = get_class_vars('A');
foreach ($b as $key => $value) {
if (!array_key_exists($key,$c)) echo $key . ' => ' . $value . '<br />';
}
What is your goal? Imo it's not very good practice (unless you really know what you are doing). Maybe it's good idea consider create some class property like "$parameters" and then create setter and getter for this and use it in this way:
class foo {
private $variables;
public function addVariable($key, $value) {
$this->variables[$key] = $value;
}
public function getVariable($key) {
return $this->variables[$key];
}
public function hasVariable($key) {
return isset($this->variables[$key]);
}
(...)
}
$var = new foo();
$var->addVariable('newVariable', 1);
$var->addVariable('otherVariable', "hello, im a variable");
And then you can use it whatever you want, for example get defined variable:
$var->getVariable('otherVariable');
To check if some var is already defined:
$var->hasVariable('someVariable')
get_class_vars() http://php.net/manual/en/function.get-class-vars.php
You question is not clear though.
$var->newVariable = 1;
there are two possible contex of above expression
1) you are accessing class public variables.
like
class foo
{
public $foo;
public function method()
{
//code
}
}
$obj_foo = new foo();
$obj_foo->foo = 'class variable';
OR
2) you are defining class variable runtime using _get and _set
class foo
{
public $foo;
public $array = array();
public function method()
{
//code
}
public function __get()
{
//some code
}
public function __set()
{
// some code
}
}
$obj_foo = new foo();
$obj_foo->bar= 'define class variable outside the class';
so in which context your question is talking about?
the code explains it better:
class Class{
$var = 0;
function getvar()
echo $this->var;
}
}
$inst1 = new Class();
// I want to change $var here to 5
$inst2 = new Class();
echo $inst2->getvar(); // should be 5
Is it possible
Static. http://php.net/manual/en/language.oop5.static.php
class MyClass {
public static $var = 0;
function setVar($value) {
self::$var = $value;
}
function getVar() {
return self::$var;
}
}
echo MyClass::$var;
MyClass::setVar(1);
echo MyClass::getVar(); //Outputs 1
You should be able to do this using a static member variable.
class foo {
private static $var;
public static setVar($value) {
self::$var = $value;
}
public static getVar() {
return self::$var;
}
}
$a = new foo;
$a::setVar('bar');
$b = new foo;
echo $b::getVar();
// should echo 'bar';
You should declare $var to be static:
A data member that is commonly
available to all objects of a class is
called a static member. Unlike regular
data members, static members share the
memory space between all objects of
the same class.
You can use static variables:
class AAA{
public static $var = 0;
function getvar() {
return AAA::$var;
}
}
AAA::$var = "test";
$a1 = new AAA();
var_dump($a1->getvar());
var_dump(AAA::$var);