i have this code:
protected $val = Zend_Registry::get('values');
Whenever I put this piece of code I get:
Parse error: syntax error, unexpected '(', expecting ',' or ';' in ...
Why is it happening?
You cannot use a function call or other dynamic expression to initialize a class property. It can only be a constant or atomic value. If you need to initialize it with a function call, you must do this instead inside the constructor.
protected $val = NULL;
public function __construct() {
$this->val = Zend_Registry::get('values');
}
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.
You can not use the return-value of a function for the initial value of a class-variable.
You can however set it in the constructor of the class.
class Myclass{
protected $val;
public function __construct(){
$this->val = Zend_Registry::get('values');
}
}
Because that looks like a class variable and you cant assign data to a class variable like that.
See here http://www.php.net/manual/en/language.oop5.properties.php
You could do it like this.
class something {
protected $_val;
public function __construct()
{
$this->_val = Zend_Registry::get('values');
}
}
Related
i have a little syntax error which i'm not able to sort out, can anyone help ?
Syntax:
Config Class:
Error:
Do not instantiate private variables like that, you should only be using them for declaring properties and simple values.
You cannot declare a private variable (declaring them a return value from a static functions at least) like that, just do it in the constructor __construct() for the object. You will get the same error for any class you do with a private variable declaration like that and setting it as a return value for any function. Try running the below in PHPFiddle and you'll get the same error.
<?php
class A {
private $hi = B::some_function('hi');
}
class B {
public static function some_function(string) {
return $string;
}
}
?>
Instead do something like:
<?php
class A {
private $hi;
public function __construct() {
$this->hi = B::some_function('hi');
}
}
class B {
public static function some_function(string) {
return $string;
}
}
?>
Your syntax is incorrect as I've seen in that picture, simply because you didn't have a closing bracket '}' for the class User.
Just try this one.
Use semicolon for every function call as shown below,
$_table = Config::get('tables/users');
$_seassionsTable = Config::get('tables/user_sessions');
It may be fix your issue.
Is it possible to define a PHP class property and assign the value dynamically using a property within the same class? Something like:
class user {
public $firstname = "jing";
public $lastname = "ping";
public $balance = 10;
public $newCredit = 5;
public $fullname = $this->firstname.' '.$this->lastname;
public $totalBal = $this->balance+$this->newCredit;
function login() {
//some method goes here!
}
}
Yields:
Parse error: syntax error, unexpected '$this' (T_VARIABLE) on line 6
Is anything wrong in the above code? If so, please guide me and if it is not possible then what is a good way to accomplish this?
You can put it into the constructor like this:
public function __construct() {
$this->fullname = $this->firstname.' '.$this->lastname;
$this->totalBal = $this->balance+$this->newCredit;
}
Why can't you do it the way you wanted? A quote from the manual explains it:
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.
For more infromation about OOP properties see the manual: http://php.net/manual/en/language.oop5.properties.php
No, you cannot set properties like that.
However: you can set them in the constructor, so they will be available if someone creates an instance of the class :
public function __construct()
{
$this->fullname = $this->firstname . ' ' . $this->lastname;
}
I get an error saying unexpected '(' when trying to do this in a class:
private $doglist = file_get_contents('dogdropdown.html');
Is that not allowed for some reason?
I also tried using a function like this:
public function getDogList(){
$list = file_get_contents('dogdropdown.html');
return $list;
}
which also didnt work. if I used include it does but doesnt inlcude it in the right place.
When you declare a class property you can only assign basic scalar values or null for variables that should reference objects. If the property needs to hold the result of some operation you either make it static or assign the result either in the constructor or a method of the class.
In order to do what you are trying to do you need the following:
class MyClass {
private $doglist;
function __construct() {
$this->doglist = file_get_contents('dogdropdown.html');
}
}
Basically what I want to do is this
class A {
public $prop = new stdClass;
}
And PHP is telling me I can't do this:
PHP Parse error: syntax error, unexpected 'new' (T_NEW) in - on line ##
What's up with that? I know that you basically can't assign a function's return value to a property in initialization, but can someone explain why is that, like the technical stuff. Thanks in advance!
Try to use class constructor:
class A {
public $prop;
public function __construct(){
$this->prop = new stdClass;
}
}
PHP manual says:
Declaration of properties 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.
class variables must be initialized with static values, e.g.
public $prop = 7; // ok
public $prop = 7+7; // ok - can be evaluated at compile time
public $prop = new stdClass; // bad, dynamic result.
public $prop = get_some_value(); //also bad, dynamic result not available at compile time
Braking my head over this.
I’m trying to assign a date() function to a property of foo() class like that.
class Foo{
public $dt = date("F-d-Y H:j:s");
function today(){} //just some empty method
};
$g = new Foo();
echo $g->dt;
I get Parse error: syntax error, unexpected '(', expecting ',' or ';' //this is date() line
I also tried that.
class Foo{
public $dt;
function today(){
echo $this->dt = date("F-d-Y H:j:s");
}
};
$g = new Foo();
$g->today();
No errors and everything works fine. So, seems to me, that we can't assign date() to a class property directly.
Please point me to a right direction. Thanks in advance.
If you provide an initialization of a class property, it has to be a constant, not a function call. If you want to compute the initial value, do it in the class constructor method.
class Foo {
public $dt;
function __construct() {
$this->dt = date("F-d-Y H:j:s");
}
}
$g = new Foo();
echo $g->dt;
From the PHP documentation:
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.