I am trying to access a static class member variable in an array.
My Code (index.php):
<?php
class Foo
{
public static $staticVar = 'test';
}
class Bar
{
public $someArray = array(
Foo::$staticVar
);
}
$cls = new Bar();
var_dump($cls->someArray);
?>
On PHP-7.0 I get this error:
PHP Fatal error: Constant expression contains invalid operations in
/var/www/html/index.php on line 12
On PHP-5.6 I get this error:
PHP Parse error: syntax error, unexpected '$staticVar' (T_VARIABLE),
expecting identifier (T_STRING) or class (T_CLASS) in
/var/www/html/index.php on line 11
I just want to have the string "test" in my array.
It´s strange that when I 'echo' out the variable it works as expected:
echo Foo::$staticVar // prints 'test'
I´m new to PHP and I can´t figure out what I´m doing wrong.
Unfortunately, you can't refer to another variable or class in the initial declaration of a class property. It's just a limitation of the language as it stands. The general workaround is to initialise the property in the constructor, e.g.
class Bar
{
public $someArray = array();
public function __construct()
{
$this->someArray = array(
Foo::$staticVar
);
}
}
On a vaguely related note, PHP 5.6 did at least make some vague headway in allowing you to define constants as basic expressions, see https://3v4l.org/6TDZV
Related
I am trying to store some validation functions inside a static configuration array. Storing functions in array seems to work, however, when I put the same code inside a class, it fails. Anyone know what's going on?
$functions = array(
'function1' => function($echo) {
echo $echo;
}
);
$functions['function1']("hello world");
// Works
//----------
class A {
public static $functions = array(
'function1' => function($echo) {
echo $echo;
}
);
}
A::$functions['function1']("hello world");
//Parse error: syntax error, unexpected 'function' (T_FUNCTION)
When I run this using PHP 7 (PHP 5 will also error out), I am getting an error that basically says expressions when instantiating class variables is not allowed. This is how this will work instead:
$functions = array(
'function1' => function($echo) {
echo $echo;
}
);
$functions['function1']("hello world");
// Works
//----------
class A {
public static $functions = [];
}
A::$functions['function1'] = function($echo) {
echo $echo;
};
A::$functions['function1']("hello world");
http://php.net/manual/en/language.oop5.properties.php
Class member variables are called "properties". You may also see them
referred to using other terms such as "attributes" or "fields", but
for the purposes of this reference we will use "properties". They are
defined by using one of the keywords public, protected, or private,
followed by a normal variable declaration. This declaration may
include an initialization, but this initialization must be a constant
value--that is, it must be able to be evaluated at compile time and
must not depend on run-time information in order to be evaluated.
If I create a simple php class that looks like this:
<?php
class Test {
public $dir = __DIR__.__FILE__;
public function __construct() {
echo $this->dir;
}
}
new Test();
When the script runs, it fails with a fatal error:
Parse error: parse error, expecting `','' or `';'' in Test.php on line 4
However, if I remove the concatenation, for example public $dir = __DIR__;, the script works. Why would concatenating magic constants in this way cause a fatal error?
While I was creating a class in php, I experienced this error:
Parse error: syntax error, unexpected '[', expecting ',' or ';' on line 5
A simple example:
<?php
class MyClass
{
public $variable["attribute"] = "I'm a class property!";
}
?>
I already had a look at Reference - What does this error mean in PHP? but this doesn't seem to fit to my case. The problem of all other existing Questions seem to rely to an old PHP Version. But I am using PHP 5.6.3!
What can I do? Am I just sightless?
You can't explicitly create a variable like that (array index). You'd have to do it like this:
class MyClass {
// you can use the short array syntax since you state you're using php version 5.6.3
public $variable = [
'attribute' => 'property'
];
}
Alternatively, you could do (as most people would), this:
class MyClass {
public $variable = array();
function __construct(){
$this->variable['attribute'] = 'property';
}
}
// instantiate class
$class = new MyClass();
I guess you should declare it the way it is shown below :
class MyClass
{
public $variable = array( "attribute" => "I'm a class property!" );
}
Make an array first. Use the code below
<?php
class MyClass
{
public $variable = array("attribute"=>"I'm a class property!");
}
?>
HOpe this helps you
You cannot declare class members like this. Also you cannot use expressions in class member declarations.
There are two ways to achieve what you are looking for :
class MyClass
{
public $variable;
function __construct()
{
$variable["attribute"] = "I'm a class property!";
}
}
or like this
class MyClass
{
public $variable = array("attribute" => "I'm a class property!");
}
Although it is allowed to access class variables using the syntax:- $object::$variable, does it hold any significance i.e. in case of accessing class variables we can use either the classname or an object of that class to access a class variable isn't it?
This operator is used to access static variables. This means that the variable is linked to the class, and not to an instance of that class. i.e. shared over all instances.
here's an example to show you what I mean:
class MyClass
{
public static $myStaticVar;
public $myObjectVar;
}
$instance1 = new MyClass();
$instance2 = new MyClass();
// normal vars are linked to an instance of a class
$instance1->myObjectVar = 'value1';
$instance2->myObjectVar = 'value2';
// statics are shared between all instances of the same class
$instance1::$myStaticVar = 'value3';
echo $instance2::$myStaticVar; // results in 'value3'!
Do you mean this?
<?php
class A {
public static $b = 'Hello World!';
}
echo A::$b;
$obj = new A();
echo $obj::$b;
Warning: I do not recommend to access a static class member using an instance variable.
This only works for PHP >= 5.3.0.
PHP <= 5.2.17 doesn't like $obj::$b and throws a syntax error:
Parse error: syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM,
expecting ',' or ';'
I have a class like
class blah extends blahblah{
private $variable = '5';
function somefunction(){
echo $variable;
}
}
this works in php 5, but not in php 4.
I get a error:
Parse error: parse error, unexpected
T_VARIABLE, expecting T_OLD_FUNCTION
or T_FUNCTION or T_VA....
I also tried with public and static. Same error.
How can I add a variable inside that class that I can access from all class functions?
private is not a valid keyword in PHP 4 change it to var $variable = '5';
also the function is wrong it should be...
class blah extends blahblah{
var $variable = '5';
function somefunction(){
echo $this->variable;
}
}
In PHP4, member variables are declared with var:
var $variable = '5';
But you still have to access it via $this->variable in your function (I think, I'm not so familiar with PHP4).
That said, if possible, upgrade! PHP4 and "OOP" is more pain than fun.
Update: Ha, found it, some documentation about Classes and Objects in PHP4.