I am not quite sure why this is happening, or how to properly explain it, but maybe someone can shed some light on this.
I have a CMS system I based off of the CodeIgniter/Opencart Framework utilizing a Registry, Controller and Module. I have run into a scenario where I have previously saved a variable to the registry as:
$this->application_page = 'current/page';
But for some reason when I call it in the application:
echo empty($this->application_page)?'yes':'no';
//Returns Yes
But.. When I Reassign it:
echo empty($this->application_page)?'yes':'no';
//Returns Yes
$page = $this->application_page;
echo empty($page)?'yes':'no';
//Returns No
A var_dump returns:
var_dump($this->application_page);
string 'current/page' (length=12)
I can get around this quite easily by just using $page but I'm curious to know why this is happening?
UPDATE:
So I messed around with the _isset function but didn't get it to work, possibly my error, possibly not.. Here is how it all works together:
class Registry {
private $data = array();
public function get($key){ return (isset($this->data[$key]) ? $this->data[$key] : NULL); }
public function set($key,$val){ $this->data[$key] = $val;
}
abstract class Controller {
protected $registry;
public function __construct($registry){ $this->registry = $registry; }
public function __get($key){ return $this->registry->get($key); }
public function __set($key,$value){ $this->registry->set($key, $value); }
}
class Applications {
private $registry;
function __construct($Registry){ $this->registry = $Registry; }
function __get($key){ return $this->registry->get($key); }
function __set($key,$val){ return $this->registry->set($key,$val); }
public function buildApplication(){
$this->application_page = 'current/page';
$application = new application($this->registry);
}
}
class Application extends Controller {
public function index(){
echo empty($this->application_page)?'yes':'no';
//Returns Yes
$page = $this->application_page;
echo empty($page)?'yes':'no';
//Returns No
}
}
Hopefully this helps?
Had a Typo, Registry's functions are not magic methods. Also $registry was declared in Applications.
The class probably didn't implement the magic __isset() method which is triggered by calling isset() or empty() on inaccessible properties.
Example:
Live demo I:
<?php
class Test {
private $a = '42';
public function __get($name) {
return $this->a;
}
}
$obj = new Test();
var_dump($obj->a); // string(2) "42"
var_dump(empty($obj->a)); // bool(true)
Implementing the __isset() method as follows (Live demo II) will yield the correct result:
public function __isset($name) {
if ($name == 'a') {
return true;
}
return false;
}
// ...
var_dump($obj->a); // string(2) "42"
var_dump(empty($obj->a)); // bool(false)
Here is a new version of your code implementing the __isset() methods: http://ideone.com/rJekJV
Change log:
Added __isset() method to Controller class which internally calls Registry::has().
Added has() method to Registy class.
[Only for testing: Object initialization and running Application::index() method.]
There was one issue (after updating your answer):
You haven't declared $registry as a member variable in the Applications class.
The code fails otherwise because it doesn't access the actual member variable (magic __set() method!)
Also, you had quite some redundancy in your code (not DRY). I hope this isn't production code ;)
Related
Say I have the following class:
class Test
{
private static $instance = false;
public static function test()
{
if(!self::$instance)
{
self::$instance = new self();
}
return self::$instance;
}
public function test1()
{
//...
}
public function test2()
{
//...
}
}
And I go about calling functions by chaining them like so:
$data = Test::test(...)->test1(...)->test2(...);
At the moment for the above method chain to work I have to keep returning $instance and I really would like it if I could return something from test2() to then be assigned to $data but I am not sure how to do this as I have to keep returning $instance in order for mt method chain to work?
If you want to chain methods, you need to return the current instance from any method which has another call chained on after it. However, it's not necessary for the last call in the chain to do so. In this case that means you're free to return whatever you like from test2()
Just bear in mind, if you return something different from test2() you'll never be able to chain anything onto it in future. For example, $data = Test::test(...)->test2(...)->test1(...); wouldn't work.
Protip: it's worth documenting your code with some comments explaining which ones are and aren't chainable so you don't forget in future.
Generally speaking if you are doing method chaining, and I'm assuming each of the tests above return your data model in a different state, and I assume that you want some data from the model itself. I would do the following:
class Test
{
private static $model;
public function test1() {
//do something to model
return $this;
}
public function test1() {
//do something to model
return $this;
}
public function finish_process() {
//process results
return $this.model;
}
}
so essentially i can do the following now:
$results = Test::test1()->finish_process();
and
$results = Test::test1()->test2()->finish_process();
You can pass the $data by its reference and you can change it or assign any data into it.
// inside class
public function test2( &$data ) {
$data = 'it will work';
}
// outside class
$data = '';
Test::test(...)->test1(...)->test2($data);
check this http://php.net/manual/en/language.references.pass.php
return $this inside test1() and test2() methods.
I am currently digging into the basics of php class / constructor.
I understand how a constructor works but not why I should use it.
For example when I have a constructor like this:
function __construct($arg1, $arg2){
$this->name = $arg1;
$this->speed = $arg2;
}
Why should I use __constructor and not a simple callback like:
function foo($arg1,$arg2){
$this->name = $arg1;
$this->speed = $arg2;
}
Thank you
Doing
$obj = new Class($var1, $var2);
And
$obj = new Class($var1, $var2);
$obj->foo($var1, $var2);
Have the same end result
By forcing values to be passed on the constructor, class can define Mandatory values it should have in order to construct a class. As in the later case, one can ignore foo.
Having a method to initialize means, one ends up having different method names, foo, init etc, constructor avoids this
The constructor is always called on object instantiation and is a known pattern.
Your second example isn't (if it's intended to perform a similar initialisation role as the constructor).
<?php
class abc {
function __construct($arg1, $arg2){
echo $arg1.' '.arg2;
}
}
$obj = new abc('manish','jangir');
?>
It will print "manish jangir" automatically when the object is created
The main purpose is to keep your code clean. With placing your initialization in the constructor you can kan be sure the variable to be used in the other function will be in valid state for example :
class Foo{
private $number;
public function setNumber($number) {
$this->number = $number;
}
public function getNumber() {
if ($this->number=== null) {
throw new RuntimeException("The Number is Null !");
}
return number;
}
}
this is the class with constructor
class Foo{
private $number;
public function __construct($number) {
$this->number = $number;
}
public function getNumber() {
if ($this->number=== null) {
throw new RuntimeException("The Number is Null !");
}
return number;
}
}
with constructor you can be sure the number will be initialized. I hope my answer is clear enough but if you have another question about my answer feel free to ask in the comment :)
I have a class 'base' and a class 'loader', which looks like this.
class base {
protected $attributes = Array();
public $load = null;
function __construct() {
$this->load = loader::getInstance();
echo $this->load->welcome(); //prints Welcome foo
echo $this->load->name; //prints Foo
echo $this->name; //doesnt print anything and i want it to print Foo
}
public function __get($key) {
return array_key_exists($key, $this->attributes) ? $this->attributes[$key] : null;
}
public function __set($key, $value) {
$this->attributes[$key] = $value;
}
}
class loader {
private static $m_pInstance;
private function __construct() {
$this->name = "Foo";
}
public static function getInstance() {
if (!self::$m_pInstance) {
self::$m_pInstance = new loader();
}
return self::$m_pInstance;
}
function welcome() {
return "welcome Foo";
}
}
$b = new base();
Now what I want is a way to store variables from loader class and access them from base class using $this->variablename.
How can I achieve this? I don't want to use extends. Any idea ?
I don't feel like you've fully understood what coding the OOP way means. And usually Singletons are code smells so I'll just warn you:
There's probably a better way of accomplish you goal. If you provide more informations we will help you out. In its current form the answer is the following; just remember that I higly discourage its implementation in your code.
Assuming that you want to access only public (and non static) loader's variables as this->varname in the base class you should just insert this line in the beginning of the base class constructor:
$this->attributes = get_object_vars(loader::getInstance());
This will basically initialize the attributes array with all the loader public vars so that via your __get() method you can access its value.
On a side note, take a look at Dependency Injection design pattern in order to avoid using Singletons.
Your __get/__set methods access $this->attributes but not $this->load.
You could e.g. do something like (pseudocode)
function __get($key) {
- if $attribute has an element $key->$value return $attribute[$key] else
- if $load is an object having a property $key return $load->$key else
- return null;
}
see also: http://docs.php.net/property_exists
You can make static variable and then you can access this variable from anywhere
public statis $var = NULL;
and you can access it like this
classname::$var;
How this stuff works?
$object->foo1()->foo2()->foo3();
I'm working in Magento php framework. In Magento functions are called as sequence.But, I don't understand how it works.Can anyone explain.
$object->foo1()->foo2()->foo3();
First, PHP will get a pointer to the foo1 method from the $object variable. It calls this method, which then returns another object. This second object (we shall call it $object2) has a method foo2(), which is also called. foo2() returns another object ($object3), which has a method foo3(), which returns whatever it likes.
In some cases, $object, $object2 and $object3 are just pointers to the same object. This means that you can keep calling methods on the same class in a nice sequence.
You can achieve this quite easily:
class Foo
{
public function nicely()
{
return $this;
}
public function formatted()
{
return $this;
}
public function chained()
{
return $this;
}
public function calls()
{
return $this;
}
}
$foo = new Foo();
$foo->nicely()->formatted()->chained()->calls();
When the methods in the class are returning values, they are returning objects of themselves. This allows you to keep calling other methods in that class in a sequence like you posted.
class myClassA {
public $str = NULL;
public function setStr( $value ){
$this->str .= $value;
return $this; // This is the key to sequencing
}
}
$myclass = new MyClassA();
$myclass->setStr("H")->setStr("E");
// If you echoed the $str variable in myClassA, you would get
// "HE"
I have a singleton factory and would like it to return a reference to the object instance so that I can use the singleton factory to destroy the instance and not have instances elsewhere in my code to survive.
Example of what I would like to be able to do:
$cat = CatFactory::getInstance();
$cat->talk(); //echos 'meow'
CatFactory::destructInstance();
$cat->talk(); //Error: Instance no longer exists
This could work:
<?php
class FooFactory
{
private static $foo;
private function __construct()
{
}
public static function getInstance()
{
return self::$foo ? self::$foo : (self::$foo = new FooFactory());
}
public static function destroyInstance()
{
self::$foo = null;
}
public function __call($fn, $args)
{
if (!method_exists(self::$foo, $fn) || $fn[0] == "_")
throw new BadMethodCallException("not callable");
call_user_func_array(array(self::$foo, $fn), $args);
}
# function hidden since it starts with an underscore
private function _listen()
{
}
# private function turned public by __call
private function speak($who, $what)
{
echo "$who said, '$what'\n";
}
}
$foo = FooFactory::getInstance();
$foo->speak("cat", "meow");
$foo->_listen(); # won't work, private function
FooFactory::destroyInstance();
$foo->speak("cow", "moo"); # won't work, instance destroyed
?>
Obviously it is a hack.
Based on the documentation for unset, I do not think that is possible. You cannot actually destroy an object, only a handle to it. If other variables are around that still hold a reference, the object will continue to live on.
You can accomplish what you want by having your Cat object enforce a private $destroyed property. PHP 5 passes objects by reference by default, so you don't have to worry about that part.
A work around would be creating a cat class
class cat
{
public $cat;
public function __construct()
{
$this->cat = CatFactory::getInstance();
}
public function __destruct()
{
CatFactory::destructInstance();
}
}
$cat = new cat();
$cat->cat->talk();
$cat->cat->talk();