Saturday 14 October 2017

php - Access constant in class





I try to access a constant in a
class:



file1.php



[...]

define('SUBDOMAIN','name');
require_once
'file2.php';
[...]



file2.php



[...]
class
Gdl_Ute extends Response_Model {
public $table =
SUBDOMAIN.'zi_utp';

public function
statuses()
{

[...]


I've
used some variants like:



public
function __construct()
{
self::$table =
SUBDOMAIN.'zi_utp';
}



But
there is always an error like:



PHP
Parse error: syntax error, unexpected '.', expecting ',' or
';'


or



syntax
error, unexpected
T_VARIABLE



Thanks
very much for any hints! I've also read other similar questions on stackoverflow, but
none has answered this problem correctly
:/



UPDATE
Now
I've test the following
solutions:



file2.php



public
$table;
public function
__construct()

{
self::$table =
SUBDOMAIN.'zi_utp';
}


OR



file1.php



define('SUBDOMAIN',$subdomain);

const
SUBDOMAIN =
$subdomain;


RESULT



Access
to undeclared static property OR
syntax error, unexpected
T_VARIABLE

class="post-text" itemprop="text">
class="normal">Answer



Use
const to define your constant. have a look href="http://php.net/manual/en/language.oop5.constants.php"
rel="nofollow">here




No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...