class - PHP - Using a constant's value to reference a data member -
I am continuously trying to access a class object's data member. I was wondering if it is possible with a syntax that I am using?
When I try to do this in the following script, I get this error: Parse error: Syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM
Class certificate {const balance = 'certificate_'; Public work __construct () {}} class ticket {public $ cert_balance = null; Public function __ composition () {$ this- & gt; Cert_balance = 'Not a chance'; Copy $ this- & gt; Cert_balance. "& Lt; br / & gt;"; }} $ Cert = new certificate (); $ Ticket = new ticket (); // It should be equal to the next code line: $ ticket-> Cert_balance = 'good'; $ Ticket- & gt; $ Cert :: BALANCE = 'OK!';
You need to separate expressions with braces as well as PHP 5.3 First, you need to refer to continuity by the class name, such as:
$ ticket-> {CERTIFICATE :: BALANCE} = 'GOOD!';
It says
As PHP 5.3.0, it is possible in context of class using a variable
In PHP 5.3.0 and above, this will work:
$ ticket-> {$ Cert :: BALANCE} = 'OK!';
Comments
Post a Comment