I want to build class that implement a lot of methods based on it's id,
so I want to have one class parent that implement the methods!
and when i want to use this class I will extend it and just override the id variable :
class parent
{
$id = "parent";
private __construct()
{
}
public static function create_instance()
{
$instance = new self();
return $instance;
}
public static function print_id()
{
echo $this->id;
}
}
class child extend parent
{
$id = "child";
}
$instance = child::create_instance();
$instance->print_id();
the result will be "parent", but I want the result to be child ?
How to do that ?
EDIT : I also tried this and got parent instead of child:
class parent1 {
private $id = "parent";
public function __construct() {
}
public static function create_instance() {
$instance = new static ();
return $instance;
}
public function print_id() {
echo $this->id;
}
}
class child extends parent1 {
private $id = "child";
}
$instance = child::create_instance ();
$instance->print_id ();
The problem is that the visibility of $id is private whereas it should be protected because print_id() is only defined on the parent; as such it can only reach its own $id.
class parent1 {
protected $id = "parent";
// ...
}
class child extends parent1 {
protected $id = "child";
}
The alternative is, of course, to override print_id() in the child class.
Currently when you call create_instance method on child class as a result instance of parent class is created not child class as you expect.
Use late static binding in parent class "create_instance" method:
public static function create_instance()
{
$instance = new static();
return $instance;
}
More details http://php.net/manual/en/language.oop5.late-static-bindings.php
Related
class Dad {
private $par1;
function __construct(int $par)
{
$this->par1 = $par;
}
}
class Child extends Dad {
private $par2;
function __construct(int $par1, int $par2)
{
$this->par2 = $par2;
parent::__construct($par1);
}
}
Having this code I have a question about calling parent::__construct($par1) in the child class. Should we call it before initializing variables or after initializing them like in my example or it does not matter ?
In PHP (Symfony 3);
I want to reference an existing object A in another object B which class extends the one of object A, like this:
class A {
private $property1;
private $property2;
public function __construct($p1,$p2){
$this->property1 = $p1;
$this->property2 = $p2;
}
}
class B extends A {
private $property3;
public function __construct($objectA,$p3){
$this = $objectA;
$this->property3 = $p3;
}
}
$a = new A('p1','p2');
$b = new B($a,'p3');
This does not work and throw the following error at the statement $this = $objectA:
Compile Error: Cannot re-assign $this
Which are documented and explain there and there. I am looking for a workaround.
You must call parent constructor and also make property1 and property2 visible in class B
<?php
class A {
private $property1;
private $property2;
public function __construct($p1,$p2){
$this->property1 = $p1;
$this->property2 = $p2;
}
public function getProperty1()
{
return $this->property1;
}
public function getProperty2()
{
return $this->property2;
}
}
class B extends A {
private $property3;
public function __construct($objectA,$p3){
parent::__construct($objectA->getProperty1(), $objectA->getProperty2());
$this->property3 = $p3;
}
}
$a = new A('p1','p2');
$b = new B($a,'p3');
See it live here: http://sandbox.onlinephpfunctions.com/code/705bf1827da2bdf10f8d961ee1cb6fbdd88bc663
As an alternative, you could use __call magic method to forward all cals to class A:
<?php
class A {
private $property1;
private $property2;
public function __construct($p1,$p2){
$this->property1 = $p1;
$this->property2 = $p2;
}
}
class B extends A {
private $property3;
private $a;
public function __construct($objectA,$p3){
$this->a = $objectA;
$this->property3 = $p3;
}
public function __call($name, $arguments)
{
return call_user_func_array(array($this->a, $name), $arguments);
}
}
$a = new A('p1','p2');
$b = new B($a,'p3');
Based on how to clone object to child class in php
Using get_object_vars on the parent object, you can get an array of properties keys and values. You can then loop through them and assign them to the child object:
<?php
class A {
protected $property1;
protected $property2;
public function __construct($p1,$p2){
$this->property1 = $p1;
$this->property2 = $p2;
}
}
class B extends A {
private $property3;
public function __construct($objectA,$p3){
//$this = $objectA;
$objValues = get_object_vars($objectA); // return array of object values
foreach($objValues AS $key=>$value)
{
$this->$key = $value;
}
$this->property3 = $p3;
echo $this->property1;
}
}
$a = new A('p1','p2');
$b = new B($a,'p3');
This does not work with private properties, they need to be at least of protected level.
I ended up managing it like that:
class B extends A{
public function __construct($objectA){
foreach($this as $k => $v){
if(isset($objectA->{$k})){
$this->{$k} = &$objectA->{$k};
}
}
}
}
Compare to #Antony answer, notice it has & in front of $objectA->{$k}: $this->{$k} = &$objectA->{$k};. As I understood it, with &, any change on $objectB of properties belonging to the extended class A applies to $objectA.
I am aware it is not perfect and quite hacky but it does the job I need. Thanks for the input given by everybody.
I created a parent class (A) and modified some of its public and protected properties.
I created a child class (B) that extends A.
I can see the parent properties at B instance after creating it.
Problem is: The inherited properties of B have the default values of A, from before I modified them.
I want B to hold the modified values of the inherited properties.
How?
class Dashboard {
protected $testBusinessesIds = '';
public function test_bids($a){
$this->testBusinessesIds = $a;
}
}
class DashboardDBHelper extends Dashboard{
protected $withoutTestBids = '';
public function __construct(){
if($this->testBusinessesIds != '')
$this->withoutTestBids = " AND B.`id`";
}
}
$d = new Dashboard();
$d->test_bids(23);
$dh = new DashboardDBHelper();
print_r($dh->withoutTestBids);
I see: '' instead of 'AND B.id'
You may need to put your property as static. Here's an example:
class A {
protected $value = 1;
protected static $staticValue = 1;
public function printStatic() {
self::$staticValue++;
echo self::$staticValue;
}
public function printNonStatic() {
$this->value++;
echo $this->value;
}
}
class B extends A {
public function printStatic() {
echo self::$staticValue;
}
public function printNonStatic() {
echo $this->value;
}
}
$a = new A();
$b = new B();
/* Class A */
$a->printStatic(); // 2
$a->printNonStatic(); // 2
/* Class B */
$b->printStatic(); // 2
$b->printNonStatic(); // 1
Static variables does not share the same class/object so if you modify the value it will be changed everywhere.
Edit: Original example removed as it was complex.
The codes provided below doesn't work. I am trying to access the methods defined in a class which is declared in the parent class.
Here is a sample code. Its not working and I'd like to know why
<?php
function & get_instance()
{
return Main::get_instance();
}
class Db{
function select($var)
{
echo $var;
}
}
class Main
{
public $db ;
public $process ;
private static $instance;
function __construct()
{
self::$instance = &$this;
$this->db = new Db ;
$this->process = Process;
}
public static function & get_instance()
{
return self::$instance;
}
}
class Process{
private $main ;
function __construct()
{
$this->main = get_instance() ;
}
function processPayment()
{
$this->main->db->select("hello");
}
}
$main = new Main ;
$main->process->processPayment();
To access members of a parent class, you will have to declare those members protected or public.
For example:
public var $db;
protected var $orders;
I have this code running on PHP 5.2.6
class Singleton {
static private $instance = false;
private $id = false;
protected function __construct() {
$this->id = uniqid();
}
static public function instance() {
if (!self :: $instance) {
self :: $instance = new self();
}
return self :: $instance;
}
public function get_id() {
return $this->id;
}
}
class Chucknorris extends Singleton {
}
echo "php version = ".phpversion()."<br>";
$singleton = Singleton::instance();
echo get_class($singleton)."<br>";
echo "singleton id = ".$singleton->get_id()."<br>";
$chucknorris = Chucknorris::instance();
echo get_class($chucknorris)."<br>";
echo "chucknorris id = ".$chucknorris->get_id()."<br>";
Here's the output
php version = 5.2.6
Singleton
singleton id = 4ea7dca7d8f23
Singleton
chucknorris id = 4ea7dca7d8f23
When I ask for an instance of Chucknorris, I always get the Singleton one. I'd like to find out a way to extend the Singleton.
I know we can use get_called_class method to do it but it comes only with PHP 5.3. Is there anyway I can extend a Singleton without redefining the design pattern in the extended classes ?
Your best bet in PHP < 5.3 is to use a Singleton Factory:
class Singleton
{
private $id = false;
public function __construct() {
$this->id = uniqid();
}
public function get_id() {
return $this->id;
}
}
class SingletonFactory
{
private static $instance_array = array();
public static function getInstance($class_name)
{
if (!isset(self::$instance_array[$class_name]))
{
self::$instance_array[$class_name] = new $class_name();
}
return self::$instance_array[$class_name];
}
}
class Chucknorris extends Singleton {}
$singleton = SingletonFactory::getInstance('Singleton');
echo get_class($singleton)."<br>";
echo "singleton id = ".$singleton->get_id()."<br>";
$chucknorris = SingletonFactory::getInstance('Chucknorris');
echo get_class($chucknorris)."<br>";
echo "chucknorris id = ".$chucknorris->get_id()."<br>";
The only downside here is that your Singleton constructor is public.. so that's a basic violation of that pattern.
Update:
Here's a version that removes the public constructor (warning: this is getting into messy/hacky/poor design territory)
class Singleton
{
private $id = false;
public function __construct() {
$back = debug_backtrace(false);
if (!isset($back[1]['class']) || $back[1]['class'] != 'SingletonFactory')
{
throw new Exception('Consturctor not available, use SingletonFactory::getInstance("CLASSNAME")');
}
$this->id = uniqid();
}
public function get_id() {
return $this->id;
}
}
class SingletonFactory
{
private static $instance_array = array();
public static function getInstance($class_name)
{
if (!isset(self::$instance_array[$class_name]))
{
self::$instance_array[$class_name] = new $class_name($class_name);
}
return self::$instance_array[$class_name];
}
}
class Chucknorris extends Singleton {}
$singleton = SingletonFactory::getInstance('Singleton');
echo get_class($singleton)."<br>";
echo "singleton id = ".$singleton->get_id()."<br>";
$chucknorris = SingletonFactory::getInstance('Chucknorris');
echo get_class($chucknorris)."<br>";
echo "chucknorris id = ".$chucknorris->get_id()."<br>";
$badchuck = new Chucknorris(); // Exception!
Why don't you simulate the get_class_function if it doesn't exist with 5.3 PHP version ?
This code may answer your question.
if (!function_exists('get_called_class')) {
function get_called_class() {
$bt = debug_backtrace();
$lines = file($bt[1]['file']);
preg_match(
'/([a-zA-Z0-9\_]+)::'.$bt[1]['function'].'/',
$lines[$bt[1]['line']-1],
$matches
);
return $matches[1];
}
}
abstract class Singleton {
private $id = false;
protected function __construct() {
$this->id = uniqid();
}
static public function instance() {
static $instances = array();
$called_class_name = get_called_class();
if (!isset($instances[$called_class_name])) {
$instances[$called_class_name] = new $called_class_name();
}
return $instances[$called_class_name];
}
public function get_id() {
return $this->id;
}
}
class Chucknorris extends Singleton {}
class Brucelee extends Singleton {}
echo "php version = ".phpversion()."<br>";
$chucknorris = Chucknorris::instance();
echo get_class($chucknorris)."<br>";
echo "chucknorris id = ".$chucknorris->get_id()."<br>";
$brucelee = Brucelee::instance();
echo get_class($brucelee)."<br>";
echo "brucelee id = ".$brucelee->get_id()."<br>";
You can redefine just the getinstance method (and the instance itself) in Chucknorris to get an instance of it instead of the parent, but I'm not exactly sure what your end goal is. Just change the extending class to:
class Chucknorris extends Singleton {
static private $instance = false;
static public function instance()
{
if (!self :: $instance) {
self :: $instance = new self();
}
return self :: $instance;
}
}
Is this what you want? And if so - what is the reason you want it? I could think of a few, but would be glad if you share you goal.
Your code will most likely work if you move static private $instance = false; to the subclass and make it protected instead of private.
You also need to replace self:: with static:: so the static var is set in the subclass.
This requires PHP 5.3 - however, this shouldn't be a problem because PHP 5.2 reached end-of-life/support (that includes security updates!) as of january 2011!