There are already many threads about this kind of problem, but for some reason i can't get it to work.
In TestClass::test(), $db is NULL.
The $db value is set in App construct and I'm trying to recover that value from an extended class function. (so i don't need to set $db everytime everywhere).
Some help would be greatly appreciated, thanks.
File : index.php
<?php
include('classes/App.class.php');
$oApp = new App();
echo TestClass::test();
?>
File : App.class.php
<?php
class App {
protected $db;
public function __construct () {
include_once("CAutoLoader.class.php");
$oCAutoLoader = new CAutoLoader();
$this->db = "someValue";
}
}
?>
File : TestClass.class.php
<?php
class TestClass extends App
{
function __construct () {
}
public static function test () {
return $db;
}
}
?>
File : CAutoLoader.class.php
<?php
class CAutoLoader {
CONST CLASS_EXTENSION = '.class.php';
public function __construct () {
spl_autoload_register(array($this, 'loader'));
}
private function loader ($className) {
include $className . self::CLASS_EXTENSION;
}
}
?>
You forgot a this in your TestClass and a static method cannot access non-static properties. Remove the static keyword and return the right value.
public function test() {
return $this->db;
}
Edit:
If you meant to retrieve the instance of the db via a static method you must declare the variable as static.
class App {
protected static $db = 'hey';
...
}
class TestCase extends App {
public static function test() {
return parent::$db;
}
}
echo TestCase::test(); // returns hey
Related
I have a core class as a collector and two subclasses stored in public variables in this core class:
class Core
{
public $cache;
public $html;
public function __construct()
{
$cache = new Cache();
$html = new Html();
}
}
class Cache
{
public function __construct()
{
}
public function store($value)
{
// do something
}
}
class Html
{
public $foo;
public function __construct()
{
$foo = "bar";
global $core;
$core->cache->store($foo);
}
}
QUESTION:
I would like to get rid of the line "global $core" and do something like:
$this->parent->cache->store($foo)
$cache should be connected to $core in some way because it is a public member of $core
I know that $html is a subclass stored as a variable and not an inheritance.
Any ideas?
Second question: Can I leave out the empty constructor in the Cache-Class?
What you can do is to use the concept of dependency injection to inject in your HTML class an object of the class Cache, and then, use it to call method store. I believe that this is a very good approach. So you can do something like this.
class Core
{
public $cache;
public $html;
public function __construct()
{
$cache = new Cache();
$html = new Html($cache);
}
}
In your class HTML:
class Html
{
public $foo;
public function __construct(Cache $cache)
{
$foo = "bar";
$cache->store($foo);
}
}
About your second question, if there is no necessity of do something in the constructor, you could just ommit it. But there is no problem to let it empty as well. So I think that it up to you.
Your object can't access caller class methods, because he do not know anything about it's caller.
You can try to pass parent when creating new object
class Core {
public $cache;
public $html;
public function __construct() {
$this->cache = new Cache($this);
$this->html = new Html($this);
}
}
class Html {
public $parent;
public function __construct($parent) {
$this->parent = $parent;
if (!empty($this->parent->cache)) {
$this->parent->cache->store();
}
}
}
Can I leave out the empty constructor - yes, you even do not have to declare __construct method at all, as all classes has it's default constructor/destructor.
I have a hard time figuring out how to add a variable value to an instantiated class in php,
I've been looking at the reflectionClass and tried to return an assigned variable, and now I'm ended up with a getter setter.
I would really appreciate some help, here's an example of my code:
class controller
{
public $models;
private $load;
public function __construct()
{
$this->load = new loader();
}
public function action_being_run()
{
$this->load->set_model('model_name');
}
}
class loader
{
public function set_model($name)
{
{controller_class}->models[$name] = new model();
}
}
The controller class is instantiated without assigning it to a variable, but just:
new controller();
And then the action is executed from within the controller class.
You could pass a reference to $this into set_model()
class controller
{
public $models;
private $load;
public function __construct()
{
$this->load = new loader();
}
public function action_being_run()
{
$this->load->set_model('model_name', $this);
}
}
class loader
{
public function set_model($name, &$this)
{
{controller_class}->models[$name] = new model();
}
}
You also need to change public $model to public $models. There are probably other ways to achieve what you want, by either extending a class or just using magic methods to access the model.
Like this:
class tester{
public function lame(){
return 'super lame';
}
}
function after(){
return 'after function';
}
$tst = new tester; $tst->afterVar = 'anything'; $tst->afterFun = 'after';
echo $wh->afterVar;
echo $wh->afterFun();
I have a class:
class My_Class {
private $playlist_table_name;
public function __construct() {
$this->playlist_table_name = "something";
require_once('markup.php');
}
}
How do I access $playlist_table_name from markup.php file?
I tried using: $this->playlist_table_name, but I get:
Using $this when not in object context
If you want to access the variable like that, you will need to mark it as public
class My_Class {
public $playlist_table_name;
public function __construct() {
$this->playlist_table_name = "something";
require_once('markup.php');
}
}
You are then going to want to instantiate the class before attempting to use it.
$MyClass = new My_Class;
echo $MyClass->playlist_table_name;
That will allow you to echo out the value.
I try to call getTest() function inside Child class.
First I initialize instance of Ext class so I assume main property $ext should now contain it. But Child class does not inherit it and obtained error message is:
Call to a member function getTest() on a non-object
Where is the issue?
<?php
$A = new Main;
class Main
{
protected $ext = null;
function __construct()
{
$this->ext= new Ext();
new Child;
}
}
class Child extends Main
{
function __construct()
{
echo $this->ext->getTest();
}
}
class Ext extends Main
{
public function getTest()
{
return "cool";
}
}
?>
I know that to solve it other way I can use:
class Child
{
private $Main;
function __construct( &$Main ) { ... }
}
but I would like to understand why that does not work..
At the moment on construct the object the atribute don't have a value yet.
You need call to the parent constructor before use the attribute
function __construct()
{
parent::__construct();
echo $this->ext->getTest();
}
Is it possible to globalize an instance which has been declared in a Class?
For example:
<?php
class Application
{
public $ioClass = new Class();
public $ioGlobalClass = new GlobalClass();
public function __construct()
{
// constructor
}
}
class Class
{
public function __construct()
{
// constructor
}
public function doSomething()
{
// is this possible?
global $ioGlobalClass;
echo $ioGlobalClass->helloWorld();
}
}
?>
So can I do global $ioGlobalClass; in this example above?
I expect not if it hasn't been instantiated anywhere. You might have more luck with a singleton:
public static $ioGlobalClass = new GlobalClass()
then:
Application::$ioGlobalClass->helloWorld();
Sure you can:
<?php
require_once('global_class.php');
class Application
{
public $ioGlobalClass;
public function __construct()
{
global $ioGlobalClass;
$this->ioGlobalClass = $ioGlobalClass;
}
...
And in global_class.php you just write:
<?php
global $ioGlobalClass;
$ioGlobalClass = new GlobalClass();