unexpected T_PUBLIC error [closed] - php

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
class Index extends Controller{
public function first_index(){
parent::__construct();
public $name = 'tiko';
$this -> view -> render('index/template','index/index');
}
}
Error:
Parse error: syntax error, unexpected T_PUBLIC in
Z:\home\localhost\www\3ddproc.ru\controllers\index.php on line 6
Line 6 - public $name = 'tiko';

you should set $name inside the class, not the function.
in the function you can set it to have any value you want, but declaration must be in the class root scope
class Index extends Controller{
public $name;
public function first_index() {
parent::__construct();
$this->name = 'tiko';
$this -> view -> render('index/template','index/index');
}
}

Public, protected, and private provide scope resolution for class functions (methods) and member variables remove public $name = 'tiko'; from inside the function instead put outside or before the function
class Index extends Controller{
public $name = 'tiko';
public function first_index(){
parent::__construct();
$this -> view -> render('index/template','index/index');
}
}

Related

Parent class always undefined [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I have a weird problem in PHP. I have two classes, one extending another. The problem is that i when I try to access a variable in the parent class (via a getVar() method), it returns undefined, even though it had been already defined in the parent's constructor.
class HttpClient
{
private $errorList;
public function __construct()
{
$errorList = [];
}
public function getHttpErrorList() { return $errorList; }
//...
}
class Twitter extends HttpClient
{
public function __construct()
{
parent::__construct();
//..
}
public function getMessages()
{
//...
var_dump($this->getHttpErrorList()); //returns undefined variable!
}
What is the cause of this problem and how do I solve it?
Defining $errorList=[] inside your constructor is defining a local variable to the constructor. I.e. variable $errorList is undefined in method getHttpErrorList() - if you want access to $errorList at the object level, you need to change it to $this->errorList inside your constructor and in method getHttpErrorList().
<?php
class HttpClient
{
private $errorList;
public function __construct()
{
$this->errorList = []; // change to $this->errorList
}
public function getHttpErrorList() { return $this->errorList; } // change to $this->errorList
//...
}
class Twitter extends HttpClient
{
public function __construct()
{
parent::__construct();
//..
}
public function getMessages()
{
//...
var_dump($this->getHttpErrorList()); //outputs array(0) {}
}
}
$twit = new Twitter();
$twit->getMessages(); // output: array(0) { }

Assign function return value into protected variable in class [duplicate]

This question already has answers here:
PHP function call in class property
(3 answers)
Closed 6 years ago.
use config;
class abc extends xyz{
protected $adminMail = config('deployment.adminemail');
public function xyz(){
......
}
}
It gives me syntax error for line protected "$adminMail = config('deployment.adminemail'); of (;)"
I know this question is not very logical but please guide where I am getting confused.
This should do the trick for you, as far as config() is implemented in the right way.
use config;
class abc extends xyz{
protected $adminMail;
public function __construct() {
$this->adminMail = config('deployment.adminemail');
}
public function xyz(){
......
}
}
Explanation:
You cannot use an expression as a default value, because class variables are initiated before runtime. Means only scalar types like a string or an integer are allowed as default value. The only way to archive this ist within the constructor.

How to use PHP class throughout application without affecting much overhead performance [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
So, I have this class that gets used over and over again throughout my app. This is just an example, not what's actually needed to be done:
File A:
class Names {
public function first() {
echo 'Bob';
}
}
That file is autoloaded into my app using spl_autoload_register and used all throughout in other pages/classes:
File B:
class LastNames {
public function __construct() {
$this->first = new Names();
}
public function last() {
echo $this->first->first().' Smith';
}
}
$names = new LastNames();
echo $names->last(); // Bob Smith
I have many files that instantiate the class Names inside the constructor.
Does this cause much of a hit on performance?
Should I be using static function instead of public function?
Is there a better way to reuse the functions inside Names over and over again inside different classes?
You have several options, using static function is one of them :
class LastNames {
public static function foo(){
// do stuff
}
}
Or you can use a singleton :
class LastNames {
private static $instance = null;
private function __construct(){
// do stuff
}
public function foo(){
// do stuff
}
public static function getInstance(){
if ( !self::$instance ){
self::$instance = new LastNames();
}
return self::$instance;
}
}
And then you can call it with :
$lastnames = LastNames::getInstance();
$lastnames->foo();

Is this a PHP OO bug? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Given the following example:
<?php
class Model
{
private $data = [];
public function __set($property, $value)
{
$this->data[$property] = $value;
}
public function __get($property)
{
if(isset($this->data[$property]))
{
return $this->data[$property];
}
throw new Exception("Error trying to access undefined data");
}
public static function all()
{
// returns all models
}
public function save()
{
// save something to database
}
}
And this class:
class Person extends Model
{
protected $name;
public static function migrateNamesToUppercase()
{
foreach(self::all() as $person)
{
$person->name = strtoupper($person->name);
$person->save();
}
}
}
Inside static method "Person::migrateNamesToUppercase" $person->name is null.
Outside static method "Person::migrateNamesToUppercase" (new Person())->name throws the expected exception.
When the class instance lives inside a static method of the same class PHP just assumes it has access to a protected property and neither __get or __set is executed! Sadly, same thing happens to private properties.
My question is: Shouldn't the behavior of the instances be the same in both contexts? Is this a known bug or just a failed PHP OO implementation?
I googled about it and found nothing
__get() is utilized for reading data from inaccessible properties.
See the PHP manual for details
It's working as defined. $name is accessible from the object so it does not use the method. If $name were private and defined in the parent class it would be inaccessible and so would use the method.

see variable from other class php [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
It's possible to do this if the class B NOT extended the class A but the class A call a new class B
class A{
public $lang;
public function __construct($lang) {
$this->lang=$lang;
}
public function new_B(){
return new B();
}
}
class B{
public function __construct() {
echo 'lang='.A::$lang;
}
}
$root=new A('eng');
$root->new_B();
You seem to have a mixup of concepts here. The $lang property of A is an instance level variable (since it is not defined as static), therefore you cannot access it statically as you are trying to. If you were to declare the variable as static then you would have access to it, but if you have multiple instances of class A that change it, it will change on the class level, rather than instance level.
Is A::$lang common to all A objects you will create? Then make this variable static. If not you can pass A::$lang as parameter to the B constructor. That is
class A{
public $lang;
public function __construct($lang) {
$this->lang=$lang;
}
public function new_B(){
return new B($this->lang);
}
}
class B{
public function __construct($lang) {
echo 'lang='.$lang;
}
}
Following is making A::$lang static:
class A{
public static $lang;
public function __construct($lang) {
self::$lang=$lang;
}
public function new_B(){
return new B();
}
}
class B{
public function __construct() {
echo 'lang='.A::$lang;
}
}
change the class B like this:
class B extends A{
public function __construct() {
echo 'lang='.$this->$lang; // you can use parent variables like this
}
}

Categories