Parse error when trying to access constant of subobject, [duplicate] - php

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 7 years ago.
I've discovered a very strange behavior of PHP. This code:
class Foo
{
const CONSTANT = 'SomeValue';
}
class Bar
{
public $tmp;
function __construct()
{
$this->tmp = new Foo;
}
}
$object = new Bar;
echo $object->tmp::CONSTANT;
Gives a parse error:
Parse error: syntax error, unexpected '::' (T_PAAMAYIM_NEKUDOTAYIM), expecting ',' or ';' in E:\OpenServer\domains\mvc.local\test.php on line 18
What a hell is that? Why it works only if I introduce an intermediate variable:
$interm = $object->tmp;
echo $interm::CONSTANT
I really don't need any additional variables.
PHP 5.6.3

You can access the class constant via the class name, not the object.
e.g. echo Foo::CONSTANT;

Related

Code Igniter REST_Controller.php get parse error [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 4 years ago.
I use codeigniter-restserver but i got error like
Parse error: syntax error, unexpected 'class' (T_CLASS), expecting identifier (T_STRING) or variable (T_VARIABLE) or '{' or '$' in xxxxxxxxxxxxx\application\libraries\REST_Controller.php on line 835
here the line
if (method_exists(Format::class, 'to_' . $this->response->format))
{
// Set the format header
$this->output->set_content_type($this->_supported_formats[$this->response->format], strtolower($this->config->item('charset')));
$output = Format::factory($data)->{'to_' . $this->response->format}();
// An array must be parsed as a string, so as not to cause an array to string error
// Json is the most appropriate form for such a data type
if ($this->response->format === 'array')
{
$output = Format::factory($output)->{'to_json'}();
}
}
else
{
// If an array or object, then parse as a json, so as to be a 'string'
if (is_array($data) || is_object($data))
{
$data = Format::factory($data)->{'to_json'}();
}
// Format is not supported, so output the raw data as a string
$output = $data;
}
Update your PHP to the leatest version.

Show page if mysql value is met [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 6 years ago.
I'm fairly new to PHP, and I don't know a lot about it. I have had a go, but I don't know where I have gone wrong.
The code:
<?php
include('config.php');
if(isset($_SESSION['username']) and $_SESSION['moderator'] {
} else {
header("location:noaccess.php");
}
?>
The error: Parse error: syntax error, unexpected '}' in E:\LocalWebHost\htdocs\forum\test.php on line 4
<?php
include('config.php');
if(isset($_SESSION['username']) and $_SESSION['moderator']) {
} else {
header("location:noaccess.php");
}
?>
In line 3, your if statement needs closing ).

multidimensional arrays in php with parse error [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 6 years ago.
<?php
array $food= array('healthy'=>
array('Salad','Vege',Pasta'),
'unhealthy'=>
array('pizza','icecream'));
echo $food['unhealthy'][1];
?>
i am writing this code but getting this error on browser:
error : Parse error: syntax error, unexpected '$food' (T_VARIABLE),
expecting '(' in C:\xampp\htdocs\foreach.php on line 2
Remove array at the starting and also there is missing single quote before Pasta.
Try this
<?php
$food = array(
'healthy'=> array('Salad', 'Vege', 'Pasta'),
'unhealthy'=> array('pizza', 'icecream')
);
echo $food['unhealthy'][1];
?>

Parse error: syntax error, unexpected '->' (T_OBJECT_OPERATOR) on line 10 [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 7 years ago.
So i get the error
"Parse error: syntax error, unexpected '->' (T_OBJECT_OPERATOR) in
/Applications/MAMP/htdocs/classes/class.ManageUsers.php on line 10",
does anyone know what part of the syntax is wrong? Below is the relevant class.
5 class ManageUsers {
6 public $link;
7
8 function __construct(){
9 $db_connection = new dbConnection();
10 $this->link = db_connection->connect();
11 return $this->link;
12 }
db_connection->connect(); should be $db_connection->connect(); notice the missing $

Parse error Unexpected (, expected , in php when assign content file to a static property [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Parse error: syntax error, unexpected ‘.’, expecting ‘,’ or ‘;’
I have this class:
<?php
class MyClass {
const DB_NAME = "MyDb";
const HOST = "localhost";
const USER = "abcdef";
const PASSWORD = "ghijklmn";
public static $MyString = file_get_contents('file.txt');
}
?>
I have no idea what is wrong with file_get_contents ?
I cannot understand what is the error says ? Why ( is unexpected ?
I read the following articles but these don't help me to solve that error:
Parse error: syntax error, unexpected T_STRING in php
Parse error T_Variable
file_get_contents shows unexpected output while reading a file
It's because you have assigned expression to variable declaration. It can only use constants.
The workaround would be like this
<?php
class MyClass {
...
public static $MyString;
...
}
MyClass::$MyString = file_get_contents('file.txt');

Categories