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 4 years ago.
Improve this question
I've created following class:
class NotFoundException extends Exception {}
class Foo{
private $path;
private $array;
public static function load($name) {
try {
return new Foo($name);
} catch (NotFoundException $unfe) {
return null;
}
}
public function __construct($name){
if (true){
$this->$path = 'public_html/'.$name.'/';
$this->$array= array('1','2','3');
}
else
throw new NotFoundException();
}
public function getArray(){
return $this->$array;
}
}
$foo = Foo::load('first');
print_r($foo->getArray());
When I run my code, I get
NOTICE Undefined variable: path on line number 18
FATAL ERROR Uncaught Error: Cannot access empty property
I have no idea what is causing the problem.
You should access your properties like $this->path and $this->array without prepending the $.
You could update your code to:
class NotFoundException extends Exception {}
class Foo{
private $path;
private $array;
public static function load($name) {
try {
return new Foo($name);
} catch (NotFoundException $unfe) {
return null;
}
}
public function __construct($name){
if (true){
$this->path = 'public_html/'.$name.'/';
$this->array= array('1','2','3');
}
else
throw new NotFoundException();
}
public function getArray(){
return $this->array;
}
}
$foo = Foo::load('first');
print_r($foo->getArray());
Related
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) { }
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();
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 9 years ago.
Improve this question
How to create public php class object inside a another class?.
I have a class called
page.php
and also
Main.php
and
Content.php
I want to call Main.php and Content.php class inside the page.php class. What is the correct way to do this.
I tried this, but not working :( . Please help.
<?php
class Page
{
public $main;
public $content;
function __construct()
{
$main=new Main_Model();
$content=new Content_Model();
}
public function Menu()
{
$load_menu=$content->Load_Menu();
...
...
}
}
?>
I can only assume you are new to OOP.
But the issue resides within you needing to access the variables from the global scope.
I am also making the assumption that you are using a framework that provides auto-loading and that these classes are actually accessible.
class Page
{
private $main;
private $content;
function __construct()
{
$this->main=new Main_Model();
$This->content=new Content_Model();
}
public function Menu()
{
$load_menu=$this->content->Load_Menu();
...
...
}
}
That should solve everything for you. Also you should define your variables as private unless you plan on exposing them for use in other places as a public interface. And even then there is discussion on using methods to access private variables.
This should fix you're issue and I've changed/fixed a few other bits like public/private variables etc. As others have said you're missing the $this-> in your construct()
<?php
class Main_Model {
public function Load_Menu() {
return "This is the menu function";
}
}
class Content_Model {
public function Load_Content() {
return "This is the main content function";
}
}
class Page {
private $mainmenu;
private $content;
function __construct() {
$this->mainmenu = new Main_Model();
$this->content = new Content_Model();
}
function Menu() {
return $this->mainmenu->Load_Menu();
}
function Content() {
return $this->content->Load_Content();
}
}
$page = new Page();
echo $page->Menu();
echo "<br />";
echo $page->Content();
?>
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.
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
}
}