Below is my simplified code where it's giving me errors. It's probably a very simple thing but it's making me confused.
class MyController extends ParentController {
public $pet_list = $this->pet_list_array();
//Parse error: syntax error, unexpected T_VARIABLE in ......
public function pet_list_array() {
return array('cat'=>'Steve\'s Cat',
'dog'=>'Fiona\'s Dog',
'lion'=>'John\'s Lion');
}
}
If I do this instead, I get a different error
public $pet_list = pet_list_array();
//Parse error: syntax error, unexpected '(', expecting ',' or ';' in.....
But if I do this public $pet_list = pet_list_array; (without the round brackets after the function name), it seems to work fine. Is this a normal behaviour? I am a bit unsure.
Your problem is that you can't make a call to a class method in the class definition. Make a __construct() method and set it there, like so:
class MyController extends ParentController {
public $pet_list;
public function __construct(){
$this->pet_list = $this->pet_list_array();
}
public function pet_list_array() {
return array('cat'=>'Steve\'s Cat',
'dog'=>'Fiona\'s Dog',
'lion'=>'John\'s Lion');
}
}
Of course, you probably don't need both $pet_list and pet_list_array() in the same class if they return the same value all the time.
Related
Configuration of a project in dev mode with WAMP.
PHP vers 5 and 7 are available.
Just trying to set the project root using filter_input. Could someone please explain why filter input for the protected and private vars inside the class reports a PARSE ERROR? However if used outside the class or inside a function of the class it works.
Is there a better way to do this so that it can be used globally? I find this is called a lot and would prefer to do it once.
$test = filter_input(INPUT_SERVER,'DOCUMENT_ROOT');
echo $test; //good
class FooBar{
protected $_test = filter_input(INPUT_SERVER,'DOCUMENT_ROOT'); //bad - Parse error: syntax error, unexpected '(', expecting ',' or ';'
private $_test2 = filter_input(INPUT_SERVER,'DOCUMENT_ROOT'); //bad - Parse error: syntax error, unexpected '(', expecting ',' or ';'
function __construct() {
}
public function getProducts(){
include_once
(filter_input(INPUT_SERVER,'DOCUMENT_ROOT').'/obj/user.php'); //good
}
}
You can not directly assign a function return value to a property in the class definition.
This is because the function could return different return values, and the class is only a blueprint which you must instantiate as an object to use.
For the objects that are created from your class definition you can initialize any property in the constructor:
class FooBar {
protected $var = null;
private $var2 = null;
function __construct() {
$this->var = func1();
$this->var2 = func2();
}
}
// no parse error
Despite of that, why do you use filter_input on an internal constant? You only need to filter input from the outside, i.e. GET/POST/SESSION content (user input), input read from files, from external APIs etc. But you don't need to use that on internal constants like the DOCUMENT_ROOT:
class FooBar {
private $_docroot = $_SERVER['DOCUMENT_ROOT'];
}
// no parse error
I have this code
class View
{
const DEFAULT_VIEWS_DIRECTORY = $_SERVER["DOCUMENT_ROOT"] . "views/";
}
but, it gives me syntax error
Parse error: Parse error: syntax error, unexpected '$_SERVER' (T_VARIABLE) in C:\xampp\htdocs\classes\View.class.php on line 17
I checked the manual and it says
The value must be a constant expression, not (for example) a variable, a property, or a function call.
is there any work around to do what I want ?, because I consume this value in the class heavily, and having this as a constant will make the class more pretty
As you noticed, Expression is not allowed as class constant value, but it doesn't stop you to initiate your default view directory once and and use it on different occasion:
class View
{
private $defaultViewDirectory;
public function __construct()
{
$this->defaultViewDirectory = $_SERVER["DOCUMENT_ROOT"] . "views/";
}
public function getDefaultViewDirectory()
{
return $this->defaultViewDirectory;
}
}
Or you could implement a Singleton pattern like:
class View
{
private $defaultViewDirectory;
private function initDefaultViewDirectory()
{
$this->defaultViewDirectory = $_SERVER["DOCUMENT_ROOT"] . "views/";
}
public function getDefaultViewDirectory()
{
if (is_null($this->defaultViewDirectory)) {
$this->initDefaultViewDirectory();
}
return $this->defaultViewDirectory;
}
}
Or if you need a static access:
class StaticView
{
private static $defaultViewDirectory;
private static function initDefaultViewDirectory()
{
self::$defaultViewDirectory = $_SERVER["DOCUMENT_ROOT"] . "views/";
}
public static function getDefaultViewDirectory()
{
if (is_null(self::$defaultViewDirectory)) {
self::initDefaultViewDirectory();
}
return self::$defaultViewDirectory;
}
}
So you could call StaticView::getDefaultViewDirectory()
I'm getting following error shown below while setting class member variables. How to set member variables in php like we normally do in c#.net
Parse error: syntax error, unexpected T_VAR, expecting T_VARIABLE ,Kindly help me out to fix it. I'm newbie in PHP
<?php
class clsCustomer{
public var $customercode;
public var $customername;
public var $customeraddress;
public function PrintCustomerDetails(){
echo $this->customercode." ".$this->customername." ".$this->customeraddress;
}
}
$obj = new clsCustomer();
$obj->customercode = 1;
$obj->customername = "Shiv";
$obj->customeraddress = "Mumbai India";
$obj->PrintCustomerDetails();
?>
Remove the var keywords from the property declarations:
class clsCustomer{
public $customercode;
public $customername;
public $customeraddress;
...
}
Further Reading
Classes and Objects
Properties
I'm translating an application written in Laravel 4.
When I tried to translate a line in a library file, I got an error, and after some time, I figured you can't set the translation directly to a variable in a library file.
The following lines
class Service {
private $test = trans('general.name');
produces the following
syntax error, unexpected '(', expecting ',' or ';'
pointing at the "private $test" line.
I'd like to add that I can normally translate and set to a variable if it's in a function, or in a different kind of file, i.e. a view.
Does anyone have an answer to this?
You can't use a function in a class variable. You should do something like:
<?php
class Service {
private $test;
public function __construct()
{
$this->test = trans('general.name');
}
}
class Service {
private $name;
public function __construct()
{
$this->name = trans('general.name');
}
public static function test() {
$service = new Service;
return $service->name;
}
}
Here your static solution:
echo Service::test();
It is not perfect solution but can be useful in some cases
T_PAAMAYIM_NEKUDOTAYIM sounds really exotic, but most certainly absolutely nonsense to me. I traced it all down to this lines of code:
<?php
Class Context {
protected $config;
public function getConfig($key) { // Here's the problem somewhere...
$cnf = $this->config;
return $cnf::getConfig($key);
}
function __construct() {
$this->config = new Config();
}
}
?>
In the constructor I create a Config object. Here's the class:
final class Config {
private static $instance = NULL;
private static $config;
public static function getConfig($key) {
return self::$config[$key];
}
public static function getInstance() {
if (!self::$instance) {
self::$instance = new Config();
}
return self::$instance;
}
private function __construct() {
// include configuration file
include __ROOT_INCLUDE_PATH . '/sys/config/config.php'; // defines a $config array
$this->config = $config;
}
}
No idea why this doesnt work / what the error means...
T_PAAMAYIM_NEKUDOTAYIM is the double colon scope resolution thingy PHP uses - ::
Quick glance at your code, I think this line:
return $cnf::getConfig($key);
should be
return $cnf->getConfig($key);
The first is the way to call a method statically - this code would be valid if $cnf contained a string that was also a valid class. The -> syntax is for calling a method on an instance of a class/object.
Just my two cents for future visitors who have this problem.
This is the correct syntax for PHP 5.3, for example if you call static method from the class name:
MyClassName::getConfig($key);
If you previously assign the ClassName to the $cnf variable, you can call the static method from it (we are talking about PHP 5.3):
$cnf = MyClassName;
$cnf::getConfig($key);
However, this sintax doesn't work on PHP 5.2 or lower, and you need to use the following:
$cnf = MyClassName;
call_user_func(array($cnf, "getConfig", $key, ...otherposibleadditionalparameters... ));
Hope this helps people having this error in 5.2 version (don't know if this was openfrog's version).
In your example
return $cnf::getConfig($key)
Probably should be:
return $cnf->getConfig($key)
And make getConfig not static
if you still need to use the double-colon then make sure your on PHP 5.3+
The error is down to an "inappropriate use" of the double colon operator:
return $cnf::getConfig($key);
as by using the :: you're attempting to call a static method of the class itself. In your example you want to call a non-static method on an instantiated object.
I think what you want is:
return $cnf->getConfig($key);
According to wikipedia, it means a "double colon" scope resolution operator.
http://en.wikipedia.org/wiki/Scope_resolution_operator
It's the name for the :: operator
Wikipedia
For anyone using Laravel. I was having the same error on Laravel 7.0. The error looked like this
syntax error, unexpected '::' (T_PAAMAYIM_NEKUDOTAYIM), expecting ';' or ','
It was in my Routes\web.php file, which looked like this
use Illuminate\Support\Facades\Route;
use Illuminate\Http\Request;
use // this was an extra **use** statement that gave me the error
Route::get('/', function () {
return view('save-online.index');
})->name('save-online.index');