I'd like to create a class (e.g. Bar) which has a private static property. This property should be an array of objects of Foo.
<?php
class Foo {
}
class Bar {
private static $classes = array(new Foo(), new Foo());
public static function testClasses() {
var_dump(self:$classes);
}
}
Bar::testClasses();
However this code throws an exception:
PHP Parse error: syntax error, unexpected 'new' (T_NEW), expecting ')' in [...]/test.php on line 8
Can somebody explain me why this is not possible?
From the docs:
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.
Your use of new to instantiate the classes in the property definition Is dependent on run-time information
Related
The code in question...
class MyClass
{
public $template_default = dirname(__FILE__)."/default.tpl";
}
Why do I get the following error when I try to use the dirname() function when defining an object property?
Parse error: syntax error, unexpected '(', expecting ',' or ';' in ...
blah blah blah
I guess object properties are not like PHP variables.
That's right. From the docs:
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.
Since dirname is a run-time function, it should be called in the constructor of the Object. So, set $template_default in the object constructor:
class MyClass {
public $template_default;
public function __construct(){
$this->template_default = dirname(__FILE__). "/default.tpl";
}
}
If you are using PHP 5.6, you can do the following:
class MyClass
{
public $template_default = __DIR__."/default.tpl";
}
PHP 5.6 allows simple scalar math and string concatenation in initialization now (docs), and __DIR__ is the same thing as dirname(__FILE__).
Otherwise, Drakes' answer is correct.
I have a Config class with:
class Config{
[...]
public static function url()
{
if(self::debug)
return "https://localhost:44300";
else
return "https://www.mysite.com";
}
[...]
}
then a class to manage Facebook logins, where I want to define a string with the callback uri:
class Fb
{
public static $login_redirecturi = Config::url() . "/login/";
[...]
}
But I can't understand why it gives an error:
Parse error: syntax error, unexpected '(', expecting ',' or ';' in [...] on line 20
How can I do?
You can't call methods on property declarations.
From the PHP docs:
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.
http://php.net/manual/en/language.oop5.properties.php
When I try to do the following I get a syntax error, unexpected T_VARIABLE. What am I doing wrong?
class myObj {
public $birth_month;
public $birthday = array('input_val' => $this->birth_month);
}
I also tried
class myObj {
public $birth_month;
public $birthday = array('input_val' => $birth_month);
}
You cannot use an expression to initialize a class property. It must be a constant value, or you must initialize it in the constructor. That's the source of your syntax error.
class myObj {
public $birth_month;
public $birthday;
// Initialize it in the constructor
public function __construct($birth_month) {
$this->birth_month = $birth_month;
$this->birthday = array('input_val' => $this->birth_month);
}
}
From the docs on class 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.
In your first attempt, using $this outside an instance method would not have been supported even baring the compile-time limitation of property initialization, since $this is only meaningful inside instance methods.
$this does not exist outside a non-static method of your class. Also, at initialization time, there is no $this yet. Initialize your array in the constuctor method.
function CharField($len)
{
return "VARCHAR($len)";
}
class ArticleModel extends Model
{
public $name = CharField(100); // Error Here
}
When I assign a public property like this with a returned value from a function, it throws the error:
PHP Parse error: syntax error, unexpected '(', expecting ',' or ';' in /var/www/test/db.php
What can the reason be?
You can only initialize properties with constants:
http://www.php.net/manual/en/language.oop5.properties.php
[Properties] 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.
So indeed, initialize them in your constructor.
Initialize the value in your constructor
According to the manual you can only assign a constant value when instantiating a class property.
Here is my class
class Databases {
public $liveresellerdb = new Database('1host1','user','pswd','db');
}
the error i am getting is
Parse error: syntax error, unexpected T_NEW in /home/abhijitnair/sandbox/newreseller/Databases.php on line 33
why this error is coming?
Properties may not be preset with runtime information.
Quoting PHP Manual:
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.
<?php
class Databases {
public static $liveresellerdb;
}
Databases::$liveresellerdb = new Database('1host1','user','pswd','db');
?>
This is how you initialise a static member...
Because you forgot to write the static keyword to actually make the property static.
In addition, you can't initialise static properties with expressions like this. Here's a workaround.
you cannot assign object during the class preperation stages, only the class instantation:
class Databases
{
public $liveresellerdb;
public function __construct()
{
$this->liveresellerdb = new Database('1host1','user','pswd','db');
}
}
anything within the constructor can be generic PHP code, outside the function and instead the class body has specific laws.
if you require the database's to be static then you have to set / access them differently.
class Databases
{
public static $liveresellerdb;
}
Databases::liversellerdb = new Database('1host1','user','pswd','db');