Calling an external class only once? - php

I have a class that using an external package to do something
class MyClass
{
public function doSomething($data){
$external = new External();
$external->doSomething($data);
}
}
This class is called from another class, for example:
class MasterClass
{
public function go(){
$data = 'whatever';
$data2 = 'whatever2';
$myClass = new MyClass();
$myClass->doSomething($data);
$myClass->doSomething($data2);
....
}
}
So in my MasterClass I am calling the doSomething function multiple times. WHich creates a new External class multiple times - which is not really necessary.
How can I get around this issue and only create the external class once?

class MyClass
{
protected $external;
public function doSomething($data){
if(!$this->external){
$this->external = new External();
}
$this->external->doSomething($data);
}
}
But read about dependency injection in php.

Move the new External() call into the constructor and store it as a property, then reference that property in the doSomething() method instead of constructing a new instance every time.
Alternatively, if you don't want to always construct a new External whenever you construct a MyClass, you could move the construction into a Lazy Load static method called something like getExternal() in External class.
The first time that method is called it would need to store a new instance of External as a property, but on subsequent calls (when the property is already set) return the same instance. This type of pattern is called Singleton Pattern in Object-Oriented Design Patterns.
You could refer to this link to know more about singleton pattern and how it is implemented.

Pass External class in constructor.
class MyClass
{
private $external;
public function __construct(External $external)
{
$this->external = $external;
}
public function doSomething($data)
{
$this->external->doSomething($data);
}
class MasterClass
{
public function go() {
$data = 'whatever';
$data2 = 'whatever2';
$external = new External();
$myClass = new MyClass($external);
$myClass->doSomething($data);
$myClass->doSomething($data2);
....
}
}

Related

Make own loadhelper function (like in CodeIgniter)

I'm trying make every day something new. Now i want to make function to dynamic load helper without include and creating new class object
In code igniter that's look like
$this->load->helper('syslog_helper');
and now we can use
syslog_helper->some_function()
file is automatic including, object is automatic created and we can use them
Question is: how can I do the same think using pure PHP ?
Like this
Create a class name load that has a method named helper, if you want it accessible with $syslog_helper then load needs to be able to call the original class so when you create an instance pass the $this as part of its constructor. Then the main class should use the magic __set method etc
Helper class to load:
class syslog_helper{
}
Loader class:
class loader{
protected $obj;
public function __construct($obj){
//save the controller instance as a property
$this->obj = $obj;
}
public function helper($class){
//require the class file - don't need to in this case as they are all in the same file for sandbox
//require_once HELPER_DIR.$class.'.php';
//create a new instance of Helper and assign it back to the controller
$this->obj->$class = new $class;
}
}
Base controller class
class foo{
public $data = [];
public $load;
public function __construct(){
//create the loader instance, pass an instance of the controller (this)
$this->load = new loader($this);
}
public function bar(){
//call the loader's helper() method with the name of the helper class
$this->load->helper('syslog_helper');
print_r($this->syslog_helper);
}
public function __set($key, $value){
//create a dynamic property
$this->data[$key] = $value;
}
public function __get($key){
//get a dynamic property
return $this->data[$key];
}
}
Call it:
(new foo)->bar();
Output:
syslog_helper Object
(
)
Sandbox
As you can see above, $this->syslog_helper gets populated with our helper class just like CI does it.
So it flows in this order:
$foo = new foo - create instance of controller, assign the loader class (with a back reference to the controller) $this->load = new loader($this);
$foo->bar() - call bar() this would be the request function in the controller, such as what the URL routes to.
$foo->load->helper('syslog_helper') - use the load property (instance of loader) to call its helper method, passing the name of the helper class as a string. Helper method should require the class file, and then create an instance of that class. new $class
$this->obj->$class = new $class; - then that instance is assigned to a dynamic property named the same as what was passed in
$this->obj->$class - Controller's __set magic method is triggered, storing instance of helper in Controler->data[$helper]
$foo->syslog_helper() - Controller's __get magic method is triggered returning Controler->data[$helper] or the instance of the helper we just created.
I just made this up, but I am sure CI is similar. You could look in the parent class of the Controllers etc.. and see how they do it.
Hope that makes sense...
One simple improvement you could make for the above code
I think CI does this, is to allow aliasing of the property... like this:
class loader{
protected $obj;
public function __construct($obj){
//save the controller instance as a property
$this->obj = $obj;
}
public function helper($class, $alias = null){
//if no alias default to the class name
if(!$alias) $alias = $class;
//require the class file - don't need to in this case as they are all in the same file for sandbox
//require_once APPPATH.'helpers/'.$class.'.php';
//create a new instance of Helper and assign it back to the controller
$this->obj->$alias = new $class;
}
}
Now if we did this in the controller's bar method:
class foo{
public function bar(){
//call the loader's helper() method with the name of the helper class
$this->load->helper('syslog_helper');
print_r($this->syslog_helper);
$this->load->helper('syslog_helper', 'syslog_helper_2');
print_r($this->syslog_helper_2);
}
Output:
syslog_helper Object
(
)
syslog_helper Object
(
)
You now have 2 instances of the helper, one named syslog_helper and the other syslog_helper_2. If we didn't alias them, the second call would simply overwrite the property in the controller leaving us with just one instance.
So you can see above we added a whole lot of flexibility with essentially 1 line of code. Big improvements don't have to be complex.
Sandboxy
Obviously you should flesh this out a bit more. By adding more things, like error checking for classes (files) that don't exist, __unset and __isset magic methods etc... But, this is the basic functionality you wanted.
Similarly you can add, model and library methods with the only real difference being the location. For that I would probably go with the magic __call method instead of 3 functions that do the same.
Implement load->model, load->library and load->helper
class loader{
protected $obj;
public function __construct($obj){
//save the controller instance as a property
$this->obj = $obj;
}
public function __call($method, $args){
//type => folder
$allowed = ['helper'=>'helpers','model'=>'models', 'library'=>'libraries'];
if(!isset($allowed[$method])) throw new Exception('Unknown method '.$method);
if(!isset($args[0])) throw new Exception('Missing required argument for method '.$method);
$class = $args[0];
$alias = isset($args[1]) ? $args[1] : $class;
//require the class file - don't need to in this case as they are all in the same file for sandbox
//require_once APPPATH.$allowed[$method].'/'.$class.'.php';
//create a new instance of Helper and assign it back to the controller
$this->obj->$alias = new $class;
}
}
Sandbox
Implement singletons
A singleton is basically re-using the same instance of the class for future calls, you can implement this with a few more changes to loader:
class syslog_helper{
public $test;
}
class loader{
protected $obj;
protected static $instances = [];
public function __construct($obj){
$this->obj = $obj;
}
public function __call($method, $args){
//type => folder
$allowed = ['helper'=>'helpers','model'=>'models', 'library'=>'libraries'];
if(!isset($allowed[$method])) throw new Exception('Unknown method '.$method);
if(!isset($args[0])) throw new Exception('Missing required argument for method '.$method);
$class = $args[0];
$alias = isset($args[1]) ? $args[1] : $class;
//if this is the first time we instantiated [$method][$alias] save it
if(!isset(static::$instances[$method][$alias])){
//require the class file - don't need to in this case as they are all in the same file for sandbox
//require_once APPPATH.$allowed[$method].'/'.$class.'.php';
//create a new instance of Helper and assign it back to the controller
static::$instances[$method][$alias] = new $class;
}
//return the saved static instance
$this->obj->$alias = static::$instances[$method][$alias];
}
}
class foo{
public $data = [];
public $load;
public function __construct(){
$this->load = new loader($this);
}
public function bar(){
$this->load->helper('syslog_helper');
print_r('bar::Test before: '.$this->syslog_helper->test."\n");
$this->syslog_helper->test = 'test';
print_r('bar:Test after: '.$this->syslog_helper->test."\n");
}
public function biz(){
$this->load->helper('syslog_helper');
print_r('biz:Test: '.$this->syslog_helper->test."\n");
}
public function __set($key, $value){
$this->data[$key] = $value;
}
public function __get($key){
return $this->data[$key];
}
}
$foo = new foo;
$foo->bar();
$foo->biz();
Output:
bar::Test before:
bar:Test after: test
biz:Test: test
Sandbox
The important thing here is that, when we call $this->load->helper('syslog_helper'); from biz() in the controller we are getting the same instance of the helper we created previously. You can tell this because the public property I added to the helper retains it's value we set in bar(). You could actually call this anywhere in your code and get the same instance with the same data stored in it, it was just easier (shorter) for examples sake to do it this way.
This is useful if you need the same helper in multiple classes, instead of creating multiple instances you can re-use them. I am not sure if CI does this right off the top of my head... lol
In this case I think it's acceptable to do them as a singleton, if you need a new copy you can just alias it and then that would be a independent instance.
One last thing I should add, is that CI probably doesn't pass the controller instance to the loader class. This is because CI calls the controller from the routing so it already has the instance of the Controller at it's disposal. And as CI is a singleton it can probably be accessed with $CI = get_instance(); inside of loader, so there is no need to pass it along in the the way I showed inside of the CI framework. Basically they just access the same data in a different way.
Cheers!

PHP Object Oriented array not being printed

Alright so I got 2 problems on my hands at the moment and those are
I want to call the function from the parent object but I am getting a lot of errors saying "Fatal error: Cannot instantiate abstract class Person"
If I call the getUserItems directly it will not do anything. There wont be anything echoed or such.
<?php
abstract class Person {
abstract function getUserItems();
}
class inventory extends Person {
protected $storage = array();
protected $item_id;
public function itemAdd($itemname) {
$storage[$this->item_id+1] = $itemname;
}
public function getUserItems() {
foreach($this->storage as $itemName=>$item_id) {
echo $itemName." ".$item_id."<br/>";
}
}
}
$user = new Person();
$user->getUserItems();
/*$user = new inventory();
$user->itemAdd("Item 1");
$user->itemAdd("Item 2");
$user->getUserItems();*/
?>
In OOP - abstract class may not be instantiated.
An abstract class is a class with both implementation and interface (pure virtual methods) that will be inherited. Interfaces generally do not have any implementation but only pure virtual functions.
So you cannot instantiate (new Person()). You must extend this abstract class and implement it's abtract functions (same way you did in inventory).
Regarding the echoing problem - inside your itemAdd function you didn't use the object's $storage member (you just used a local $storage variable). You should use it as the object's memebr, using $this->storage:
public function itemAdd($itemname) {
$this->storage[$this->item_id+1] = $itemname;
}
Note that I'm not so sure how you managed the $this->item_id member because you didn't set/changed it in the code.
If you only want to add new items to the storage member you could use:
$this->storage[] = $itemname;
This will make sure every new item will be added to the $storage array.
ad1)
Person is abstract class so you can not create object of that class.
ad2)
You must use $this
so not:
$storage[$this->item_id+1]
but
$this->storage[++$this->item_id]
Here I fixed another bug which was $this->item_id+1
Abstract class can not access directly but access from other class.
abstract class Person {
abstract function getUserItems();
}
class inventory extends Person {
protected $storage = array();
protected $item_id=2;
public function itemAdd($itemname) {
$storage[$this->item_id+1] = $itemname;
}
public function getUserItems() {
foreach($this->storage as $itemName=>$item_id) {
echo $itemName." ".$item_id."<br/>";
}
}
}
$user = new inventory();
$user->getUserItems();
$user = new inventory();
$user->itemAdd("Item 1");
$user->itemAdd("Item 2");
$user->getUserItems();

PHP new static($variable)

$model = new static($variable);
All these are within a method inside a class, I am trying to technically understand what this piece of code does. I ran around in the Google world. But can't find anything that leads me to an answer. Is this just another way of saying.
$model = new static $variable;
Also what about this
$model = new static;
Does this mean I'm initializing a variable and settings it's value to null but I am just persisting the variable not to lose the value after running the method?
static in this case means the current object scope. It is used in late static binding.
Normally this is going to be the same as using self. The place it differs is when you have a object heirarchy where the reference to the scope is defined on a parent but is being called on the child. self in that case would reference the parents scope whereas static would reference the child's
class A{
function selfFactory(){
return new self();
}
function staticFactory(){
return new static();
}
}
class B extends A{
}
$b = new B();
$a1 = $b->selfFactory(); // a1 is an instance of A
$a2 = $b->staticFactory(); // a2 is an instance of B
It's easiest to think about self as being the defining scope and static being the current object scope.
self is simply a "shortcut name" for the class it occurs in. static is its newer late static binding cousin, which always refers to the current class. I.e. when extending a class, static can also refer to the child class if called from the child context.
new static just means make new instance of the current class and is simply the more dynamic cousin of new self.
And yeah, static == more dynamic is weird.
You have to put it in the context of a class where static is a reference to the class it is called in. We can optionally pass $variable as a parameter to the __construct function of the instance you are creating.
Like so:
class myClass {
private $variable1;
public function __construct($variable2) {
$this->variable1 = $variable2;
}
public static function instantiator() {
$variable3 = 'some parameter';
$model = new static($variable3); // <-this where it happens.
return $model;
}
}
Here static refers to myClass and we pass the variable 'some parameter' as a parameter to the __construct function.
You can use new static() to instantiate a group of class objects from within the class, and have it work with extensions to the class as well.
class myClass {
$some_value = 'foo';
public function __construct($id) {
if($this->validId($id)) {
$this->load($id);
}
}
protected function validId($id) {
// check if id is valid.
return true; // or false, depending
}
protected function load($id) {
// do a db query and populate the object's properties
}
public static function getBy($property, $value) {
// 1st check to see if $property is a valid property
// if yes, then query the db for all objects that match
$matching_objects = array();
foreach($matching as $id) {
$matching_objects[] = new static($id); // calls the constructor from the class it is called from, which is useful for inheritance.
}
return $matching_objects;
}
}
myChildClass extends myClass {
$some_value = 'bar'; //
}
$child_collection = myChildClass::getBy('color','red'); // gets all red ones
$child_object = $child_collection[0];
print_r($child_object); // you'll see it's an object of myChildClass
The keyword new is used to make an object of already defined class
$model = new static($variable);
so here there is an object of model created which is an instance of class static

PHP maintain class state

I'd like to have a library class that maintains state across the same request. My use case is that I want to pass 'messages' to the class, and then call them at any time from a view. Messages can be added from any part of the application.
I had originally done this via static methods, which worked fine. However, as part of the lib, I also need to call __construct and __destruct(), which can't be done on a static class.
Here's a very simple example of what I am trying to do:
class Messages
{
private static $messages = array();
public function __construct()
{
// do something
}
public function __destruct()
{
// do something else
}
public static function add($message)
{
self::$messages[] = $message;
}
public static function get()
{
return self::$messages;
}
}
I can then add messages anywhere in my code by doing
Messages::add('a new message');
I'd like to avoid using static if at all possible (testability). I have looked at DI, but it doesn't seem appropriate, unless I'm missing something.
I could create a class (non-static) instead, but how do I then ensure that all messages are written to the same object - so that I can retrieve them all later?
What's the best way to tackle this?
I looks like you could benefit from using the Singleton pattern - it is designed for an object that must have only one instance throughout a request. Basically, you create a private constructor and a static method to retrieve the sole instance. Here is an example of a singleton that will do what you describe.
<?php
class Messages
{
private static $_instance;
private $_messages = array();
private function __construct() {
// Initialize
}
static public function instance() {
if (! self::$_instance) {
self::$_instance = new self();
}
return self::$_instance;
}
public function add_message( $msg ) {
$this->_messages[] = $message;
}
public function get_messages() {
return $this->_messages;
}
private function __destruct() {
// Tear-down
}
}
$my_messages = Messages::instance();
$my_messages->add_message( 'How now, brown cow?' );
// ...
$your_messages = Messages::instance();
$msgs = $your_messages->get_messages();
echo $your_messages[0]; // Prints, "How now, brown cow?"
Since the constructor is private, you can only create a Messages object from within a method of the object itself. Since you have a static method, instance(), you can create a new Messages instance from there. However, if an instance already exists, you want to return that instance.
Basically, a singleton is the gatekeeper to its own instance, and it stubbornly refuses to ever let more than one instance of itself exist.
Sounds like you are wanting to do a Singleton class. This will create an instance in one class and allow you to access that same instance in another class. Check out http://www.developertutorials.com/tutorials/php/php-singleton-design-pattern-050729-1050/ for more information.
How about making it a singleton class?
class Messages
{
// singleton instance of Messages
private static $instance;
public function __construct() { ... }
public static function getInstance()
{
if (!self::$instance)
{
self::$instance = new Messages();
}
return self::$instance;
}
}
This would ensure that all your messages get written to the same object, and also allow you to call __construct and __destruct
What you need is the Singleton pattern:
final class Singleton {
// static variable to store the instance
private static $instance = NULL;
// disable normal class constructing
private function __construct() {}
// instead of using the normal way to construct the class you'll use this method
public static function getInstance() {
if (NULL === self::$instance) {
self::$instance = new self;
}
return self::$instance;
}
// disable external cloning of the object
private function __clone() {}
}
// get the instance across some of your scripts
$singleton = Singleton::getInstance();
Sounds a bit like you want a singleton, although as an anti-pattern I'd avoid it.
You could do a full static class where every static member calls a self::_isBuilt(); method to do your construct elements. Destruct is a little trickier.
The best case for your needs might be a normal (non-static) class that you build right away and then access from a global... not super neat, but allows construct/destruct and members, and your statics to use $this which could be helpful. If you don't like the global variable, you could also wrap it in a method (a trick used in JS a fair bit) but it's not really any neater.
As a normal global class:
$myClass=new myClass();
//Access anywhere as:
globals['myClass']->myFunction(..);
Wrapped in a function
function my_class() {
static $var=null;
if ($var===null) $var=new myClass();
return $var;
}
//Access anywhere as:
my_class()->myFunction(..);

Initiate a class by calling a function that returns an instance of that class - PHP?

class foo(){
function bar()
{
$classInstance = $this->createClassInstance($params);
$result = $classInstance->getSomething();
}
function createClassInstance($params)
{
require 'path/to/class.php';
$myClass = new Class;
$myClass->acceptParams($params['1']);
$myClass->acceptMoreParams($params['2']);
.... lots more params
return $myClass;
}
}
Can I initiate a new class by calling a method that returns a class object? The class in question has lots of parameters and I need to call it multiple times within bar() so I thought it would be neater to do it that way, but I can't get it working and want to check if it's possible + good practice?
That's called factory class (Factory OO Design Pattern).
How it should be done in PHP: http://www.php.net/manual/en/language.oop5.patterns.php
What I think you're describing is the Factory pattern, but you're using parameters to set the class variables just like you would in a constructor, so why not just use that?
Edit:
Ah, if you're using the same parameters for the most part then you definitely want the Factory pattern. Just use a static function to return an instance of the class, and put it inside the type of class you're returning:
class MyClass
{
public static function factory($params)
{
$myClass = new MyClass;
$myClass->acceptParams($params['1']);
$myClass->acceptMoreParams($params['2']);
//.... lots more params
return $myClass;
}
}
class foo(){
function bar()
{
$classInstance = MyClass::factory(param1, param2);
}
}

Categories