How to set Class member variables in php - php

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

Related

php filter_input parse error inside class

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

Error referring static variable inside static function in a class in PHP 7.4.2

I am learning PHP and using PHP 7.4.2 (using XAMPP) version to try out examples. I am trying to access the static variable from a static function using self::$vaiablename
But getting the following error:
Parse error: syntax error, unexpected 'self' (T_STRING) in C:\xampp\htdocs\php-getting-started\classes\CountryRepository.php on line 18
<?php
require 'Country.php';
require 'State.php';
class CountryRepository {
private static $countries = array();
protected static function init() {
$countries = array();
array_push($countries,
new Country('Austria','at',array(new State('Styria'), new State('Burgandy'))));
array_push($countries,
new Country('United States','usa',array(new State('California'), new State('Maryland'))));
array_push($countries,
new Country('Luxembourg','lu'))
self::$countries = $countries;
}
public static function getCountries() {
if(count(self::$countries) === 0) {
self::init();
}
return self::$countries;
}
}
?>
Can anyone please help me with this?
In your case there is a missing semicolon after the last array_push. PHP interprets this as array_push()self which leads to the above error.
new Country('Luxembourg', 'lu'))
^^^
vs
new Country('Luxembourg', 'lu'));
^^^

how to define a constant member of a class with $_SERVER superglobals?

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()

How to assign function's output array to a variable in PHP?

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.

PHP Class $_SERVER Variable a Property

I'm developing a class and I have this structure:
class userInfo {
public $interval = 60;
public $av_langs = null;
public $ui_ip = $_SERVER['REMOTE_ADDR'];
public $ui_user_agent = $_SERVER['HTTP_USER_AGENT'];
public $ui_lang = null;
public $ui_country = null;
// non-relevant code removed
}
But when executing the script I get this error:
Parse error: syntax error, unexpected T_VARIABLE in
D:\web\www\poll\get_user_info\get_user_info.php on line 12
When I changed the 2 $_SERVER vars to simple strings the error disappeared.
So what's the problem with $_SERVER in declaring class properties?
Thanks
Use this code as a guide:
public function __construct() {
$this->ui_ip = $_SERVER['REMOTE_ADDR'];
$this->ui_user_agent = $_SERVER['HTTP_USER_AGENT'];
}
Property can be declared only with value, not expression.
You can create __construct() method, where you can initialize properties in any way.
So what's the problem with $_SERVER in declaring class properties?
You can't preset class properties with variables nor with function calls.
Here is some in-depth discussion on why: Why don't PHP attributes allow functions?
The bottom line however is, it's simply not possible.

Categories