Initializing PHP Class Property Declarations With Simple Expressions Yields Syntax Error

Initializing PHP class property declarations with simple expressions yields syntax error

PHP doesn't do such operations at compile-time; you cannot assign calculated values to constants, even if all operators are constants themselves. Default values of class members are treated the exact same way. I encountered this behaviour as I tried to assign powers of two to constants:

class User {
const IS_ADMIN = 1;
const IS_MODERATOR1 = 1 << 1; // Won't work
const IS_MODERATOR2 = 0x02; // works
}

php constant in data member of a class

the cause for this error is that PHP does not allow expressions in class member declarations, even if only constant elements are used.

so private $r = self::ABC ." d"; is not allowed, while private $r = self::ABC; is ok, as Gautam3164 answered.

more details for example in this answer:
Initializing PHP class property declarations with simple expressions yields syntax error

PHP class not working

You cannot assign another variable to a var inside the class.

class GeoIP extends Page 
{
public $fcEnable;

function __construct($fcEnable = null)
{
$this->fcEnable = $fcEnable
}

}

issues with creating a new object in a class

You should do it in your class constructor:

class Matcher{

private $user1
private $user2;

function __construct($user1, $user2){

$this->user1 = new Person($firstName, $lastName, $zipCode, $hairColor, $job, $eyeColor, $height, $weight, $dateOfBirth, $children, $education, $ethnicity, $faith, $language, $bodyType);
$this->user2 = new Person();
}

}

But in addition I don't see any point why you create new objects here as you in constructor also assign other objects (or variables) to properties user1 and user2 .

Also for user2 you had incorrect syntax private $user2= Person; without new operator

PHP set class property to a std object in class defination

You cannot assign non-constant values to class properties during declaration. From http://php.net/language.oop5.properties

...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.

You will need to use a class method to initialise the property, eg

class Foo {
private static $Obj;

private static getObj() {
if (null === self::$Obj) {
self::$Obj = (object) ['bizz', 'bazz'];
}
return self::$Obj;
}

PHP string concatenation within class definition

The default values for properties must be constant in the source, e.g. strings and array literals. No expressions.

Use the __construct() method for anything more complicated.

Getting an error trying to initialize this public class variable using dirname() outside a method

You cannot have expressions in the variable declarations. You can only use constant values. The dirname() may not appear in this position.

If you were to use PHP 5.3 you could use:

  public $currentDir = __DIR__ ;

Otherwise you will have to initialize $this->currentDir in the __constructor.

PHP property decleration accepts array and do not accept any of other expressions that could be evaluated at the compilation process

The short answer to your question is that strlen() is a function while array() is a keyword.

The critical difference to understand is that keywords always reference the same thing regardless of context.

From php.net:

These words have special meaning in PHP. Some of them represent things which look like functions, some look like constants, and so on - but they're not, really: they are language constructs. You cannot use any of the following words as constants, class names, function or method names.

Functions, on the other hand, could be defined differently depending on where you are calling them.

Consider this simplistic example:
First a file we'll call "functions.php".

//functions.php
namespace My_Project_Namespace;

function strlen($string){
return 10; //In my project, all strings are length 10! 10 is a nice round number...
}

In this file, I am overriding the built-in strlen() function with another one. This is possible because my function is defined inside a namespace (in this case, My_Project_Namespace).

Now consider your file, but this time let's put it in our namespace (you should be name-spacing all your functions and classes)

//Test.php
namespace My_Project_Namespace;

Class Test {
public $vars=strlen("random"); // Fails
}

strlen() has 2 definitions depending on the namespace. Since knowing the current namespace depends on runtime information the compiler cannot know which to use for initialization in the class. Even if you didn't define a custom strlen() function you still couldn't do this because knowing that there isn't another version strlen() also depends on runtime information!

array() is a totally different beast. It is a keyword, you cannot define another meaning for array() so the compiler doesn't have to worry about one existing.

Set an objects property equal to an instance of another object

PHP doesn't allow you to use expressions in class definitions (which is standard in class definitions in many languages). You can do a couple of things:

public function __construct() {
$this->session = Session::instance();
}

//Dependency Injection
public function __construct(Session $session) {
$this->session = $session;
}

Syntax Error T_Variable

In php you cannot use any operator, or reference a variable, during class property declarations. You'd have to initialize the property value in the class constructor :

class CSS {
public $base = '/public/stylesheets';
public $global;

public function __construct() {
$this->global = $this->base . '/global.css';
}
}

Which is specified in this php manual quote :

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.



Related Topics



Leave a reply



Submit