PHP: call child constructor from static method in parent - php

I want to have a static method in a parent class that creates instances of whatever subclass i call this method on.
An example to make this more clear:
class parent {
public static method make_objects($conditions){
for (...){
// here i want to create an instance
// of whatever subclass i am calling make_objects on
// based on certain $conditions
}
}
}
class sub extends parent{
...
}
$objects = sub::make_objects($some_conditions);

As of php 5.3 you can use the static keyword for this
<?php
class A {
public static function newInstance() {
$rv = new static();
return $rv;
}
}
class B extends A { }
class C extends B { }
$o = A::newInstance(); var_dump($o);
$o = B::newInstance(); var_dump($o);
$o = C::newInstance(); var_dump($o);
prints
object(A)#1 (0) {
}
object(B)#2 (0) {
}
object(C)#1 (0) {
}
edit: another (similar) example
<?php
class A {
public static function newInstance() {
$rv = new static();
return $rv;
}
public function __construct() { echo " A::__construct\n"; }
}
class B extends A {
public function __construct() { echo " B::__construct\n"; }
}
class C extends B {
public function __construct() { echo " C::__construct\n"; }
}
$types = array('A', 'B', 'C');
foreach( $types as $t ) {
echo 't=', $t, "\n";
$o = $t::newInstance();
echo ' type of o=', get_class($o), "\n";
}
prints
t=A
A::__construct
type of o=A
t=B
B::__construct
type of o=B
t=C
C::__construct
type of o=C

I think you want something like this:
class parent {
public static function make_object($conditionns) {
if($conditions == "case1") {
return new sub();
}
}
}
class sub extends parent {
}
Now you can create an instance like this:
$instance = parent::make_object("case1");
or
$instance = sub::make_object("case1");
But why would you want all the sub classes to extend the parent? Shouldn't you much rather have a parent for your models (sub classes) and then a factory class, that creates the instances for this models depending on the conditions given?

Umm, wouldn't that be:
class sub extends parent {
public static function make_objects($conditions) {
//sub specific stuff here
//....
}
}

make the parent class an abstract class and make the parent method also an abstract
abstract static class parent {
abstract function make_method() {
// your process
}
}
class child extends parent {
public function __construct() {
parent::make_method();
}
}

Related

PHP OOP inheritance issue

I have an inheritance issue. Consider I have a parent class A and a derived class B from A and a derived Class C from A again.
Let's say, class B & C both override method seed() from class A. Also clases A & B & C all implement a static function seedAll().
My question is: How do I call seedAll for B & C from the seedAll function of A without explicitly specifying the class B & C, so that in the future, should I have a class D similar to class B & C, I need not modify the seedAll function of class A to call the seedAll functions of classes B, C & D.
abstract class A
{
public static function seedAll() {
//How do I call seedAll of all the derived classes of Seeder?
}
abstract protected function seed();
// // Common method
// public function printOut() {
// print $this->getValue() . "\n";
// }
}
class B extends A
{
private $name;
function __construct($name) {
$this->name = $name;
}
public static function seedAll() {
$seeder1 = new B("name1");
$seeder1 = new B("name2");
}
function seed() {
echo $this->name;
}
}
class C extends A
{
private $name;
function __construct($name) {
$this->name = $name;
}
public static function seedAll() {
$seeder1 = new C("name1");
$seeder1 = new C("name2");
}
function seed() {
echo $this->name;
}
}
See how to obtain all subclasses of a class in php for how to get all the subclasses. You can then loop over them and call their seedAll() methods.
abstract class A
{
public static function seedAll() {
$subclasses = getSubclassesOf("A");
foreach ($subclasses as $class) {
$class::seedAll();
}
}
abstract protected function seed();
// // Common method
// public function printOut() {
// print $this->getValue() . "\n";
// }
}
getSubclassesOf function:
function getSubclassesOf($parent) {
$result = array();
foreach (get_declared_classes() as $class) {
if (is_subclass_of($class, $parent))
$result[] = $class;
}
return $result;
}

PHP Use child variable to create a class in parent class

I have 3 classes:
Class A - Parent Class
Class B - Child Class
Class C - Class to be used in Class A
I want to use functions from class C using variables from my Child class.
<?php
class A
{
public function __construct()
{
$this->load();
}
public function load()
{
$class = new C();
$class->test = $this->test;
$this->c = $class;
}
}
class B extends A
{
public function __construct()
{
parent::__construct();
}
}
class C
{
public function display()
{
echo $this->test;
}
}
$b = new B();
$b->test = 1;
$b->c->display();
Your problem is here:
$class->test = $this->test;
You are attempting to use a property that is not yet defined, because when you do this:
$b->test = 1;
the constructor has already been called, and there's nothing in your classes to update C with the value of B's test property.
You can solve this in a couple of different ways.
1) Send the value in B's constructor, and pass it down the entire chain:
class A
{
public function __construct($test)
{
$this->load($test);
}
public function load($test)
{
$class = new C();
$class->test = $test;
$this->c = $class;
}
}
class B extends A
{
public function __construct($test)
{
parent::__construct($test);
}
}
class C
{
public function display()
{
echo $this->test;
}
}
$b = new B(123);
$b->c->display();
2) Add a method to B that will update C's property:
<?php
class A
{
public function __construct()
{
$this->load();
}
public function load()
{
$class = new C();
$this->c = $class;
}
}
class B extends A
{
public function __construct()
{
parent::__construct();
}
public function setTest($test)
{
$this->c->test = $test;
}
}
class C
{
public function display()
{
echo $this->test;
}
}
$b = new B();
$b->setTest(123);
$b->c->display();
Or perhaps a combination of both.

Php, inheritance, late static binding, unexpected calling chain

consider this code:
class C
{
public function get()
{
echo 'C';
static::get();
}
public function save()
{
self::get();
}
}
class B extends C
{
public function get()
{
echo 'B';
static::get();
}
}
class A extends B
{
public function get()
{
echo 'A';
}
}
$x = new A();
$x->save();
it echoes CA while I was expected CBA
To get this to work the way you want, reverse the logic - get your save to call the static::get() so it will start at the top of the inheritence tree; and use calls to parent::get() in each inherited class in the tree (except the base level) before echoing its own output
class C
{
public function get()
{
echo 'C';
}
public function save()
{
static::get();
}
}
class B extends C
{
public function get()
{
parent::get();
echo 'B';
}
}
class A extends B
{
public function get()
{
parent::get();
echo 'A';
}
}
$x = new A();
$x->save();
Demo

PHP Class calling base class extended class function

I have this Base class:
class Base
{
public $extA;
public $extB;
function __construct()
{
}
public function Init()
{
$this->extA = new ExtA();
$this->extB = new ExtB( $this );
}
public function Test()
{
return 'Base Test Here!';
}
}
class ExtA extending the Base Class
class ExtA extends Base
{
public function Test()
{
return 'ExtA Test Here!';
}
}
class ExtB extending the Base Class too
class ExtB extends Base
{
private $base;
public function __construct( $base )
{
$this->base = $base;
}
public function Test()
{
return 'ExtB calling ExtA->Test()::' . $this->base->extA->Test();
}
}
$base = new Base();
$base->Init();
var_dump( $base->Test() );
var_dump( $base->extA->Test() );
var_dump( $base->extB->Test() );
I try to call the ExtA class Test() function from the ExtB,
both of ExtA and ExtB is exnteding the Base class.
My question is : is this ok, or have a better, faster solution for this?
The extends is necessary too?
Or simply enough like this
class ExtA
{
...
}
class ExtB
{
...
}
Thanks!
This is weird way of OOP.
The Base class should not know anything about its children so we shall go more correct way. Let's implement Decorator pattern:
interface IExt
{
public function test();
}
abstract class ExtDecorator implements IExt
{
protected $instance;
public function __construct(IExt $ext)
{
$this->instance = $ext;
}
}
class ExtA extends ExtDecorator
{
public function test()
{
return 'ExtA::test here and calling... ' . $this->instance->test();
}
}
class ExtB extends ExtDecorator
{
public function test()
{
return 'ExtB::test is here and calling... ' . $this->instance->test();
}
}
class Base implements IExt
{
public function test()
{
return 'Base::test here!';
}
}
class Printer
{
public static function doMagic(IExt $ext)
{
echo $ext->test()."\n";
}
}
Printer::doMagic($base = new Base);
// Base::test here!
Printer::doMagic($extA = new ExtA($base));
// ExtA::test here and calling... Base::test here!
Printer::doMagic(new ExtB($extA));
// ExtB::test is here and calling... ExtA::test here and calling... Base::test here!
You can play further any way you want

PHP - Which class invoked my static method?

I need to know what kind invokes a static method, without sending as parameter
class foo
{
public static function test($clase)
{
echo "Class invoke:" . FUNCTION();
}
}
class A { public function x { foo::test(); } }
class B { public function y { foo::test(); } }
class C { public function z { foo::test(); } }
You can use late static bindings and get_called_class() (PHP >= 5.3) if you make all of your classes extend foo, like this:
class foo
{
public static function test($clase)
{
echo "Class invoke:" . get_called_class();
}
}
class A extends foo { public function x() { self::test(''); } }
class B extends foo { public function y() { self::test(''); } }
class C extends foo { public function z() { self::test(''); } }
With these objects:
$a = new A; $a->x();
$b = new B; $b->y();
$c = new C; $c->z();
You'll get as output:
Class invoke:A
Class invoke:B
Class invoke:C

Categories