Separate static variable for every child - php

I want to do some cache in my ORM
class Base {
static public $v=array();
static public function createById($id){
if(!array_key_exists($id, static::$v)){
static::$v[$id] = new static; //Get from DB here. new static is just example
}
return static::$v[$id];
}
}
class User extends Base{
}
class Entity extends Base{
}
But now cache is merged
var_dump(User::createById(1));
var_dump(Entity::createById(1));
results
object(Model\User)#4 (0) {
}
object(model\User)#4 (0) {
}
If I made
class Entity extends Base{
static public $v=array();
}
class User extends Base{
static public $v=array();
}
I get what I need:
object(Model\User)#4 (0) {
}
object(model\Entity)#5 (0) {
}
Is it possible to do it without declaration in every class?

If its that important that you don't re-declare the property in each child class, the only solution I can think of is, which isn't exactly what you wanted, but it should get you the same functionality, is sharing the same property on the base class to store the cache for all the child classes, but using the child class name as a key in the cache array:
class Base {
public static $v=array();
public static function createById($id){
$called = get_called_class();
if (!isset(self::$v[$called])) {
self::$v[$called] = array();
}
$class_cache = &self::$v[$called];
if(!array_key_exists($id, $class_cache)){
$class_cache[$id] = new static;
}
return $class_cache[$id];
}
}
Yes, its not pretty... but AFAIK, what you asked for isn't possible.

Related

How to access the method from trait after changing its visibility in the exhibiting class? [duplicate]

here is the class structure. I want Observer:callme() to be callable from Children too.
class Observer
{
protected callme()
{
}
}
class Parent extends Observer
{
function createChild()
{
$this->callme(); // this is OK
return new Child ($this);
}
}
class Child
{
private $this myParent;
public function __constructor ($myParent)
{
$this->myParent = $myParent;
}
public function __destroy()
{
$this->myParent->callme(); // FAIL!
}
}
so how to make FAIL work? (without making it public, because its only for used inside "Parent" and its "Children")
The problem is that a protected method is only accessed from the same class or the class children. What you can do is extend your Child class from Parent, like this:
class Child extends Parent
{
public function __constructor ()
{
parent::__constructor();
}
public function __destroy()
{
$this->callme(); // Should work!
}
}
Or just change the method to public.
And, btw, is this code some kind of real code that you will use? That constructor receiving the parent object seems to be so wrong. What are you trying to accomplish?
protected means that you can call that method only from the same class and from subclasses. What you want to do is not possible. The protected keyword would be pointless if you could call these methods from everywhere.
In C++ there is the friend keyword to achieve what you want: you could define Child as friend of Observer (this has to be done from within Observer), and then you can call all methods in Observer (including private and protected) from within methods of Child. But such a keyword does not exist for PHP.
My comment on your question explains why it doesn't work. This answer shows a way to accomplish what you asked based upon your clarification that MyChild should not extend MyParent.
This is a hack example that makes it work by exploiting the fact that php doesn't care if you call protected methods on other instances than yourself as long as you share the ancestor of the protected method.
I had to change the code some to make it valid php. __constructor is not the name of a php constructor.
hacky.php
<?php
class Observer
{
protected function callme()
{
echo 'I was called from ' . get_called_class(), PHP_EOL;
}
}
class MyParent extends Observer
{
public function createMyChild()
{
$this->callme(); // this is OK
return new MyChild ($this);
}
}
class MyChild extends Observer // hackey extends
{
private $myMyParent;
public function __construct($myMyParent)
{
$this->myMyParent = $myMyParent;
$this->myMyParent->callme();
}
}
$p = new MyParent;
$c = $p->createMyChild();
Result:
$ php hacky.php
I was called from MyParent
I was called from MyParent
I think I found the solution:
class Parent extends Observer
{
function createChild()
{
$this->callme(); // this is OK
return new Child (function() { $this->callme(); });
}
}
class Child
{
private $gatewayFunction;
public function __constructor (Closure $gatewayFunction)
{
$this->gatewayFunction = $gatewayFunction;
}
public function __destroy()
{
$this->gatewayFunction->__invoke();
}
}
Who is going to crap himself? :)

Way to get the output of an abstract function

I have an abstract class with an abstract method 'run'. Child classes extending this implements run and returns a bool as output.
Is there a way that I can get the status of run (true/false) method in the abstract class.
I want this as I am trying to add few statistics as how many classes failed/passed to execute the run method. I already have many classes extending this and don't want to add anything into those and get these statistics for free.
abstract class parent {
// I need the status of the run method in here
public abstract function run();
}
class child extends parent {
public function run() {
if (implementation) {
return true;
} else {
return false;
}
}
}
Help appreciated.
Define a non-abstract method in the parent that calls the abstract method and gets the result.
abstract class parent {
private $run_result;
public function run() {
$this->run_result = $this->run_internal();
}
abstract protected function run_internal();
}
class child extends parent {
protected function run_internal() {
if (implementation) {
return true;
} else {
return false;
}
}
}

Multi level inheritance in PHP

I'm trying to access and modify data that is in a parent class which is a child of another class.
I've a parent class
class GrandParent {
protected $data = 1;
public function __construct() {}
public function getData() {
return $this->data;
}
}
Below is my first level child
class Child extends GrandParent {
protected $c1Data;
public function __construct() {
$this->c1Data = parent::getData();
$this->c1Data = 2;
}
public function getData() {
return $this->c1Data;
}
}
If I try to instantiate the Child class and do getData(), I get 2 which is normal. I've another class that inherits Child.
class GrandChild extends Child {
protected $c2Data;
public function __construct() {
$this->c2Data = parent::getData();
}
public function getData() {
return $this->c2Data;
}
}
The problem is that if I try to instantiate GrandChild I and get the data I'm getting null. Is it possible to make my GrandChild class inherit $c1Data = 2 and work with it. I want also to be able to use the Child and GrandParent classes on their own and not be abstract.
You're getting NULL because __constructor of a Child class is not invoked and that's why c1Data property is NOT SET. You should explicitly call for Child __constructor:
class GrandChild extends Child {
protected $c2Data;
public function __construct() {
// here
parent::__construct();
$this->c2Data = parent::getData();
}
public function getData() {
return $this->c2Data;
}
}
This is how you add two integers and display the result with Multi
Level Inheritance in PHP.
<?php
/*
Inheritance:
multiple classes
Parent class/child class
senior and junior
child class extends some data or functions of parent class
child class has its own functions
child class can access all public and protected data and functions
*/
//Multi Level Inheritance Every class extends other class
//Parent Class
class A{
//data
var $a;
function setA()
{
$this->a=10;
}
}
//child class
class B extends A{
var $b;
function setB()
{
$this->b=20;
}
}
class Addition extends B{
function add()
{
$this->setA();
$this->setB();
return $this->a+$this->b;
}
}
class Print1 extends Addition{
function print()
{
$this->add();
print("a=".$this->a);
print("<br/>b=".$this->b);
print("<br/>Addtion:".$this->add());
}
}
//make object
$obj1=new Print1();
$obj1->print();
/*
Make Subtraction, multiplication and division classes and print the values as
a=10
b=20
Addtion=30
Subtraction=-10
Multiplication=200
Division:0.5
*/
?>

PHP Change parent class at runtime?

I have a parent class that depends on whether child class are instantiated.
class GoogleApp {
protected $auth_token;
public function __construct($scopes) {
$this->auth_token = $scopes;
}
}
class Gmail extends GoogleApp {
public function __construct() {
print_r($this->auth_token);
}
}
$googleApp = new GoogleApp('gmail'); // Change the actual class for all child instances
$gmail = new Gmail();
The idea is that all the children use the same auth_token (which is generated on whether the child classes are used - as of now, I'm just manually adding them to whether I included them in my code). Since I have quite a few child classes (like Calendar or Drive), do I have to inject the parent into each child instance or is there an easier way?
If I understand your request correctly, you're pretty close, you just need to declare your property as static.
class FooParent
{
protected static $scope = null;
public function __construct($scope)
{
self::$scope = $scope;
}
public function getScope()
{
return self::$scope;
}
}
class FooChild extends FooParent
{
public function __construct()
{
if (self::$scope === null) {
throw new Exception('Must set scope first.');
}
}
}
$parent = new FooParent('foo');
$child = new FooChild();
echo $child->getScope(), "\n"; // prints "foo"

accessing properties of an externally initialized parent class from a child class

A parent class is constructed from outside the child class, thus, it's constructor cannot be called from inside the child. How should one go about accessing properties of the parent from the child in this case.
Example:
class MyParent {
protected $args;
protected $child;
public function MyParent($args=false){
$this->args=$args;
$this->child=new MyChild();
}
public function main(){
$this->child->printArgs();
}
}
class MyChild extends MyParent{
public function MyChild(){}
public function printArgs(){
Echo "args: ".$this->args['key']." = ".$this->args['value']."\n";
}
}
$parent=new MyParent(array('key'=>'value'));
$parent->main();
Empty variables are returned when run:
jgalley#jgalley-debian:~/code/otest$ php run.php
args: =
__construct() is the constructor. You are using a variant from ancient PHP4-times.
You instanciate two completely different objects, therefore of course the property $args is completely independent.
abstract class MyParent {
protected $args;
public function __construct($args=false){
$this->args=$args;
}
public function main(){
$this->printArgs();
}
abstract public function printArgs();
}
class MyChild extends MyParent{
public function printArgs(){
Echo "args: ".$this->args['key']." = ".$this->args['value']."\n";
}
}
$$object=new MyChild(array('key'=>'value'));
$object->main();
This at least works, but a problem is, that I don't know exactly what are the design goals. Because it seems to be a kind of cli-Application you should have a look at existing solutions to get an idea, how it could get solved.

Categories