Trying to use objects that extend singletone, but something I can't do.
How to call method from extended class?
How to show 13 non 12 with singleton?
class SingletonTest
{
protected static $_instance;
private function __construct(){}
private function __clone(){}
public static function getInstance() {
if (null === self::$_instance) {
self::$_instance = new self();
}
return self::$_instance;
}
public function test2(){
return 12;
}
}
class ExtendSingleton extends SingletonTest
{
public function test2() {
return 13;
}
}
$b = ExtendSingleton::getInstance();
echo $b->test2(); //12
You will get what you want if you use static binding keyword "static" instead of "self"
class SingletonTest
{
protected static $_instance;
private function __construct(){}
private function __clone(){}
public static function getInstance() {
if (null === static::$_instance) {
static::$_instance = new static();
}
return static::$_instance;
}
public function test2(){
return 12;
}
}
class ExtendSingleton extends SingletonTest
{
public function test2() {
return 13;
}
}
$b = ExtendSingleton::getInstance();
echo $b->test2(); //13
$a = SingletonTest::getInstance();
echo $a->test2(); //13
exit;
But as You see in the above example this way a class which You will call first to "getInstance" will take place to store its instance in the $_instance field.
There is no way to create a base singleton class and inherit singleton behavior.
public static function getInstance()
{
static $instances = array();
$calledClass = get_called_class();
if (!isset($instances[$calledClass]))
{
$instances[$calledClass] = new $calledClass();
}
return $instances[$calledClass];
}
So this should work for you:
(So first normally functions are public so you can use them if you extend from another class! And the you have to make an object from ExtendSingleton not from SingletonTest since ExtendSingleton exdend's -> SingletonTest and not the other way.)
<?php
class SingletonTest {
protected static $_instance;
public function __construct() {
}
public function __clone() {
}
public static function getInstance() {
if (null === self::$_instance) {
self::$_instance = new self();
}
return self::$_instance;
}
public function test2(){
return 12;
}
}
class ExtendSingleton extends SingletonTest {
public function test2() {
return 13;
}
}
$b = new ExtendSingleton();
echo $b->test2(); //13
?>
Output:
13
I've tested bicccio's solution and it works
class SingletonTest
{
protected static $_instances = [];
private function __construct(){}
private function __clone(){}
public static function getInstance()
{
$calledClass = get_called_class();
if (!isset(self::$_instances[$calledClass]))
{
self::$_instances[$calledClass] = new $calledClass();
}
return self::$_instances[$calledClass];
}
public function test2(){
return 12;
}
}
class ExtendSingleton extends SingletonTest
{
public function test2() {
return 13;
}
}
$b = ExtendSingleton::getInstance();
echo $b->test2(); //13
$a = SingletonTest::getInstance();
echo $a->test2(); //12
You can extend singleton class in php using late static binding the whole process is well discussed in this question.
Creating the Singleton design pattern in PHP5
Related
I am trying to create a library for codeigniter that uses pthread, everything works fine but when i want to assign a value to an array this don't work with traditional $a['key'] = 'val';
Small Test Exemple : ( updated )
class Test {
protected $core;
protected $stack;
public function init(){
$this->stack = new Test_Stack();
$this->core = new Test_Core($this->stack);
}
public function do_test(){
return $this->core->assign();
}
}
class Test_Stack extends Stackable {
protected $a;
function __construct(){
$this->a = array();
}
protected function test(){ // Call from other class extends Threads
$this->a['key1'] = 'NOWORK';
print_r($this->a); // THIS RETURN NOTHING
$this->a = array_merge($this->a, array('key1' => 'WORK'));
print_r($this->a); // NOW THIS GOOD RETURN Key1..
}
public function run(){}
}
class Test_Core {
protected $thread;
protected $stack;
function __construct($s){
$this->stack = $s;
}
public function assign(){
$this->thread = new Test_Thread($this->stack);
$this->thread->start();
$this->thread->join();
}
}
class Test_Thread extends Thread{
protected $stack;
function __construct($s){
$this->stack = $s;
}
public function run(){
$this->stack->test();
}
}
I write this basic code without testing but it's the same structure of my lib and need this to extends test_stack and add or change test function for exemple.
Even if works now, I would understand why I can't assign my array normally ?
Or rather, what am I doing wrong?
Working in Mine :
class Stack {
protected $a;
function __construct(){
$this->a = array();
}
function test(){
$this->a['key1'] = 'NOWORK';
print_r($this->a); // THIS RETURN NOTHING
$this->a = array_merge($this->a, array('key1' => 'WORK'));
print_r($this->a); // NOW THIS GOOD RETURN Key1..
}
}
$obj = new Stack();
$obj->test();
Output :
Array
(
[key1] => NOWORK
)
Array
(
[key1] => WORK
)
OK array isn't thread safe, should be used stackable, this do the trick :
Libraries/Test.php :
class Test {
protected $core;
protected $stack;
public function init(){
$this->stack = new Test_Stack();
$this->core = new Test_Core($this->stack);
}
public function do_test(){
$this->core->assign();
$this->core->assign();
$this->core->assign();
}
public function get_a(){
return $this->core->get_a();
}
}
class Test_Array_Stack extends Stackable {
public function run(){}
}
class Test_Stack extends Stackable {
protected function test($a){
$a[] = 'WORK';
}
public function run(){}
}
class Test_Core {
protected $thread;
protected $stack;
protected $a;
function __construct($s){
$this->stack = $s;
$this->a = new Test_Array_Stack();
}
public function assign(){
$this->thread = new Test_Thread($this->stack, $this->a);
$this->thread->start();
$this->thread->synchronized(function($thread){
$thread->wait();
}, $this->thread);
return $this->a;
}
public function get_a(){
return $this->a;
}
}
class Test_Thread extends Thread{
protected $stack;
protected $a;
function __construct($s, $a){
$this->stack = $s;
$this->a = $a;
}
public function run(){
$this->stack->test($this->a);
$this->synchronized(function($thread){
$thread->notify();
}, $this);
}
}
You can extends Test_Stack to create lib extension more useful for my project, like this on other file Test_Info.php :
require_once APPPATH.'libraries/Test.php';
class Test_Info extends Test {
function init(){
$this->stack = new Test_Info_Stack();
$this->core = new Test_Core($this->stack);
}
}
class Test_Info_Stack extends Test_Stack {
protected function test($a){
parent::test($a);
$a[] = 'INFO';
}
}
And usage on controller:
function index(){
//without extension
$this->load->library('Test');
$this->test->init();
$this->test->do_test();
print_r($this->test->get_a());
//with extension
$this->load->library('Test_Info');
$this->test_info->init();
$this->test_info->do_test();
print_r($this->test_info->get_a());
}
It took me some time, i hope it will help someone and LuckyBurger thank you for the explanation link.
While cloning an object, I need to perform the same initializations that happen during the object construction.
Can I do this?
public class MyClass {
protected $myVar;
public function __construct()
{
$this->myVar = 0
}
public function __clone()
{
$this->__construct();
}
}
You can do that just fine
class MyClass {
protected $myVar;
public function __construct()
{
echo "constructing!\n";
$this->myVar = 0;
}
public function __clone()
{
echo "cloning!\n";
$this->__construct();
}
}
$a = new MyClass();
$b = clone $a;
Output
constructing!
cloning!
constructing!
in this below class i want to use class like with static methods and for use class methods without create new object from parent.
for example:
<?php
class Permission
{
protected $permission = false;
protected $id = 0;
public static function __construct()
{
return new static;
}
public function user( $id )
{
$this->id = $id;
}
public function check()
{
$this->permission = true;
}
public function item( $item )
{
return $item;
}
}
$bar = Permission::user(100)->item("HELLO");
print_r($bar);
this code not working and have problem. how to resolve this class problem?
That will not work because user method is not static, try changing this two methods, and this is good way of generating objects
public function __construct($id)
{
$this->id = $id;
}
public static function user( $id )
{
return new static($id);
}
I'd suggest you a singleton pattern, like this
class Permission
{
static protected $permission = false;
static protected $id = 0;
private static $_instance = null;
private function __construct () { }
public static function getInstance()
{
if (self::$_instance === null) {
self::$_instance = new self;
}
return self::$_instance;
}
public static function user( $userId )
{
self::$id = $userId;
return self::$_instance;
}
public static function check()
{
self::$permission = true;
return self::$_instance;
}
public static function item( $item )
{
return $item;
}
}
$bar = Permission::getInstance()->user(100)->item("HELLO");
print_r($bar);
You can chain methods in 'dynamic' classes by returning $this at the end of method (remember, you have a static).
class A {
public function someMethod()
{
// some code
return $this
}
public function otherMethod()
{
// some code
return $this
}
$a = new A();
$a->someMethod()->otherMethod();
}
I have an extended class with an overriden method doSomething().
For some reason the inherited class' method never runs only the base one.
class cDemoClass {
public static function getInstance() {
static $instance = null;
if ($instance === null)
$instance = new cDemoClass();
return $instance;
}
private function __construct() {
}
protected function doSomething() {
echo 'do something';
}
public function call_me() {
$this->doSomething();
}
}
class cDemoClassEx extends cDemoClass {
protected function doSomething() {
echo 'do something differently';
}
}
$baseclass = cDemoClass::getInstance();
$baseclass->call_me();
echo '<br/>';
$extendedclass = cDemoClassEx::getInstance();
$extendedclass->call_me();
result:
do something
do something
The second one should be "do something differently" at least that's what I'm expecting.
Can anyone tell me what I'm doing wrong? Thanks
In this case, you need using late static binding (5.3+). Change in parent method getInstance line :
$instance = new cDemoClass();
to
$instance = new static();
You will get:
do something
do something differently
Read more about this feature here: http://www.php.net/manual/en/language.oop5.late-static-bindings.php
Because cDemoClassEx::getInstance(); is still returning new cDemoClass();. You have to also overwrite the getInstance() method:
class cDemoClass {
public static function getInstance() {
static $instance = null;
if ($instance === null)
$instance = new cDemoClass();
return $instance;
}
private function __construct() {
}
protected function doSomething() {
echo 'do something';
}
public function call_me() {
$this->doSomething();
}
}
class cDemoClassEx extends cDemoClass {
public static function getInstance() {
static $instance = null;
if ($instance === null)
$instance = new cDemoClassEx();
return $instance;
}
private function __construct() {
}
protected function doSomething() {
echo 'do something differently';
}
}
$baseclass = cDemoClass::getInstance();
$baseclass->call_me();
echo '<br/>';
$extendedclass = cDemoClassEx::getInstance();
$extendedclass->call_me();
You have to override with the cDemoClassEx::getInstance() and change this line
$instance = new cDemoClass();
into
$instance = new cDemoClassEx();
You will also need to declare the cDemoClass::__construct() as protected or simply override it in cDemoClassEx.
class Foo {
public static function foobar() {
self::whereami();
}
protected static function whereami() {
echo 'foo';
}
}
class Bar extends Foo {
protected static function whereami() {
echo 'bar';
}
}
Foo::foobar();
Bar::foobar();
expected result foobar actual result foofoo
to make matters worse, the server is restricted to php 5.2
All you need is a one-word change!
The problem is in the way you call whereami(), instead of self:: you should use static::. So class Foo should look like this:
class Foo {
public static function foobar() {
static::whereami();
}
protected static function whereami() {
echo 'foo';
}
}
In another word, 'static' actually makes the call to whereami() dynamic :) - it depends on what class the call is in.
Try to use singleton pattern:
<?php
class Foo {
private static $_Instance = null;
private function __construct() {}
private function __clone() {}
public static function getInstance() {
if(self::$_Instance == null) {
self::$_Instance = new self();
}
return self::$_Instance;
}
public function foobar() {
$this->whereami();
}
protected function whereami() {
print_r('foo');
}
}
class Bar extends Foo {
private static $_Instance = null;
private function __construct() {}
private function __clone() {}
public static function getInstance() {
if(self::$_Instance == null) {
self::$_Instance = new self();
}
return self::$_Instance;
}
protected function whereami() {
echo 'bar';
}
}
Foo::getInstance()->foobar();
Bar::getInstance()->foobar();
?>
Don't you have to overwrite the parent function foobar() too?
class Foo {
public static function foobar() {
self::whereami();
}
protected static function whereami() {
echo 'foo';
}
}
class Bar extends Foo {
public static function foobar() {
self::whereami();
}
protected static function whereami() {
echo 'bar';
}
}
Foo::foobar();
Bar::foobar();