PHP: Using a class's var content in another array? [duplicate] - php

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;
}

Related

How to call a function on a class variable? [duplicate]

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;
}

PHP: Is it possible to instantiate objects inside of an array definition? [duplicate]

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;
}

PHP Can I define a variable protected variable inside class? [duplicate]

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;
}

Is it possible to define a class property value dynamically in PHP?

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;
}

Parsing error in PHP (Zend Framework)

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');
}
}

Categories