PHP class methods cascading - php

I have seen many php scripts in which you can cascade class method to produce a specific functionality for example as in codeigniter when you load a view you would type
<?php
class Posts extends Controller
{
function index() {
$this->load->view("BlaBla");
}
}
?>
I am not completely sure if these are cascaded methods or what butI don't have enough experience to hack the codeigniter core and figure out myself.
Can anyone tell me how to do this or guide me to some tutorial or so

Every method must return its class instance, like:
class A{
function B(){
//do stuff
return $this;
}
function C(){
//do stuff
return $this;
}
function D(){
return $this->B()->C()->B()->B()->C();
}
}
or you can build your own cahining class to chain anything:
class Ch{
static private $i;
public static function i($arg){
if (!self::$i instanceof self){
self::$i = new self();
}
self::$i->data=$arg;
return self::$i;
}
function __call($name,$args){
array_unshift($args,$this->data);
$this->data=call_user_func_array($name,$args);
return $this;
}
function get(){
return $this->data;
}
}
echo Ch::i('Hello world')->trim('Hld')->str_repeat(5)->substr(5,7)->strtoupper()->get();

Related

PHP chained class methods merged with other classes

I start with an example.
Mold class
<?php
class Mold {
public $current;
function merge($first, $second) {
$this->current = $first . $second;
return $this;
}
function phone() {
$this->current = '' . $this->current . '';
return $this;
}
function __toString() {
return $this->current;
}
}
function mold() {
return new Mold();
}
In action
This works as expected.
echo mold()->merge('sada', 'asdsa')->phone();
Problem
I have one or more classes with methods that wants to be available as well.
Plugin class
class MyPlugin {
function link() {
// Code
}
function currency($number) {
// Code
}
}
class MyPlugin2 {
// Other methods
}
Dream code
The exact change of events below may not make any sense. Anyway, what I intend to do is the following.
echo mold()->merge('sada', 'asdsa')->currency(45)-link();
Call mold() which creates a new instance of the Mold class.
Chain merge() which is used from the Mold class.
currency() or link() method does not exist in the Mold class. Instead they should be loaded from one of the plugins.
In conclusion
I know I can extend a class but it does not really solve the problem because there can be more than one plugin classes.
I know I can create instances of the plugin classes, but they somehow need to be aware by the Mold class.
Append methods to a class comes to mind as well as merge classes.
Technically you could use __call() for this. Your "main" class could contain/track a set of plugin instances and any unknown method call could be looked up and delegated, if available in a plugin.
I wouldn't recommend this though. Fluent APIs are often brittle and can get inconvenient even with one class. Involving multiple classes in fluent calls might quickly to add up to a confusing mess. A mess which even IDE can't help you (or another person working with code) with since it has no clue about all the magic happening.
I would highly recommend to look into alternative patterns for an API like this.
A concept that you might try to apply is the Composition Over Inheritance.
A solution could be to compose your current Moldobject and pass the two plugins instances so that it could use them on its methods, like so:
<?php
class Mold {
public $current;
public function __construct(Plugin1 $plugin1, Plugin2 $plugin2) {
$this->plugin1 = $plugin1;
$this->plugin2 = $plugin2;
}
public function merge($first, $second) {
$this->current = $first . $second;
return $this;
}
public function phone() {
$this->current = '' . $this->current . '';
return $this;
}
public function __toString() {
return $this->current;
}
public function link() {
$this->plugin1->link();
return $this;
}
}
function mold() {
return new Mold(new Plugin1(), new Plugin2());
}
Another solution could be to create a MoldHandler class of some sort, which has a Mold object as a property together with the other plugins, which could be used inside of this directly.
public class MoldHanlder {
protected $mold;
protected $plugin1;
protected $plugin2;
public function __contruct($mold, $plugin1, $plugin2) {
$this->mold = $mold;
$this->plugin1 = $plugin1;
$this->plugin2 = $plugin2;
}
public function merge() {
$this->mold = $this->mold->merge();
return $this;
}
public function link() {
$foo = $this->plugin1->method();
return $this;
}
...
}
You could also just instantiate the classes inside the construct, but if you are planning about writing unit tests, dependency injection is the way to go
I think that the first approach is way better because you avoid yourself to re-write some code of the Mold class, which is unnecessary
N.B. this is a really raw solution proposed just to let you better understand the idea I would opt for
cheers
Interesting ideas.
The one that solves the problem like I had in my mind was to use traits like #vivek_23 suggested in a comment.
Here is a complete example of how it works
<?php
// Core methods
class MoldCore {
public $current;
function merge($first, $second) {
$this->current = $first . $second;
return $this;
}
function phone() {
$this->current = '' . $this->current . '';
return $this;
}
function __toString() {
return $this->current;
}
}
// Plugin 1
trait Plugin1 {
public function yaay() {
$this->current .= ' Plugin1 yaay';
return $this;
}
}
// Plugin 2
trait Plugin2 {
public function hello() {
$this->current = str_replace('Plugin1', 'Modified', $this->current);
return $this;
}
public function world() {
$this->current .= ' Plugin2 world';
return $this;
}
}
// Glue plugins with MoldCore
class Mold extends MoldCore {
use Plugin1, Plugin2;
}
$mold = new Mold();
echo $mold->merge('sada', 'asdsa')->phone()->yaay()->hello();

return type of container from subclass

I have a PHP library which I don't want to edit, and implement to my code by extending/overriding some methods. But I'm stuck with chainability. For example:
class MomentPHP extends Moment {
public $uniqueSettings;
public function formatJS(){
return parent::format($this->uniqueSettings);
}
}
class Moment {
public function startOf(){
//some code
return $this;
}
}
I want to do this:
$momentphp = new MomentPHP();
$dateStart = $momentphp->startof('month')->formatJs();
And the way to do this is overriding all the methods in the child class inside MomentPHP to return itself.
Is there any other simple way to do this? like using _call or something?
Edit: Found one way to do this:
Remove the inheritance,
Create a instance variable of parent class,
use __call method to switch between classes.
Like this:
class MomentPHP {
private $instance = null;
public $uniqueSettings;
public function __construct(){
$this->instance = new Moment();
}
public function __call($method,$args){
if(in_array($method, get_class_methods($this))){
call_user_func(array($this,$method),$args);
else
call_user_func(array($this->instance,$method),$args);
return $this;
}
public function formatJS(){
return $this->instance->format($this->uniqueSettings);
}
}
class Moment {
public function startOf(){
//some code
return $this;
}
}
Is there any better way?
One proper way to do this is:
class MomentPHP {
private $instance = null;
public $uniqueSettings;
public function __construct(){
$this->instance = new Moment();
// settings etc.
}
public function __call($method,$args){
$result = NULL;
if(in_array($method, get_class_methods($this))){
$result = call_user_func(array($this,$method),$args);
else
$result = call_user_func(array($this->instance,$method),$args);
if($result instanceof Moment)
$this->instance = $result;
return $this;
}
public function format(){
return $this->instance->format($this->uniqueSettings);
}
}
Updating the instance from the method result is the key operation, and using $this instead of $this->instance allows you to use the extender class in every call. So you can override the function while using other methods in the parent class with chaining ability.

Why use a return when calling parent?

I am learning OO PHP and I was experimenting with using a parent::method in a child class. I noticed i had to use an "extra" return for the output of the parent method to show up. Could someone explain me why this is?
This is the code I used and in the code I made a comment.
class ShopProduct {
public $productnumber;
public function __construct($productnumber) {
$this->productnumber = $productnumber;
}
public function getSummary(){
return $this->productnumber;
}
}
class BookProduct extends ShopProduct {
public function __construct($productnumber) {
parent::__construct($productnumber);
}
public function getSummary() {
return parent::getSummary(); // if i dont use return it doesnt work? why is that?
// parent::getSummary(); is not enough it seems.
}
}
$product = new BookProduct(11111);
echo $product->getSummary();
?>
public function getSummary() {
return parent::getSummary(); // if i dont use return it doesnt work? why is that?
// parent::getSummary(); is not enough it seems.
}
Replace parent::getSummary() with any other function or method call:
public function getSummary() {
foo();
}
Of course you wouldn't expect getSummary to return anything in this case, right? Just because the method you're calling is parent::... doesn't change anything about this behaviour. It does not return automagically, because you may want to do something like this:
public function getSummary() {
$summary = parent::getSummary();
return "Book: $summary";
}
BTW, if the only thing your method does is call its parent, you can leave out the entire method. In other words, this:
class BookProduct extends ShopProduct {
public function __construct($productnumber) {
parent::__construct($productnumber);
}
public function getSummary() {
return parent::getSummary();
}
}
is exactly the same as this:
class BookProduct extends ShopProduct { }

PHP OOP Multicall

i have seen in some libraries something like this :
$this->getResponse()->setRedirect($returnUrl);
How is this 'multicall' done, or, how should the class be build to do something like this?
I think :
class greeting
{
public function hi()
{
public function howAreYou()
{
echo 'How are you?';
}
}
}
$greet = new greeting;
$greet->hi()->howAreYou();
But i think it's not so good, i would better use something like extends, but i don't know. Thx for your suggestions.
If this is a class instance calling itself, it is called "method chaining".
In PHP, can be done by using return $this; note that this is a very different mechanism than class inheritance - it doesn't really make sense to treat them as interchangeable.
See also: https://stackoverflow.com/search?q=method+chaining+php
getResponse() is returning a class instance which has a setRedirect() method.
Example:
class Foo
{
public function getResponse()
{
$redirect = new Bar();
return $redirect;
}
}
class Bar
{
public function setRedirect($returnUrl)
{
// do something
}
}
$foo = new Foo();
$foo->getResponse()->setRedirect("returnUrl");
No.
All you have to do is return self at very end of each function.
So Your example would be like>
class greeting
{
public function hi()
{
echo "Hi";
return $this;
}
public function howAreYou()
{
echo 'How are you?';
return $this;
}
}
$greet = new greeting;
$greet->hi()->howAreYou();
Or even:
$greet->hi()->howAreYou()->hi()->howAreYou();
class stutter{
public function a(){
echo 'h';
return $this;
}
public function b(){
echo 'hello world!';
}
}
$var=new stutter();
var->a()->b();
Output is:
h hello world
Chaining methods is not the same as declaring functions within a method... in fact the latter will spit an error (not the function declaration, but the way you're calling it). In order to chain a method, just have it return the object itself:
Class chainableObject
{
public $name=null;
public function __construct($name='')
{
$this->name=$name;
return $this;
}
public function setName($name)
{
$this->name = $name;
return $this;//makes chainable
}
public function greet()
{
echo 'Hello, '.$this->name;
return $this;
}
}
$chain = new chainableObject('Frank')->greet();//outputs: Hello, frank
The explanation: All methods return the instance itself, so basically, read the last line of the snippet like this [create object with name:Frank]=>call method greet on the return value of this action. Since the return value is $this, the object that has a greet method, that's what will happen... easy, for more info: just google php method chaining

Method Chains PHP OOP

Commonly, in a lot of frameworks, you can find examples of creating a query using the query builder. Often you will see:
$query->select('field');
$query->from('entity');
However, in some frameworks you can also do it like this
$object->select('field')
->from('table')
->where( new Object_Evaluate('x') )
->limit(1)
->order('x', 'ASC');
How do you actually do this kinds of chains?
This is called Fluent Interface -- there is an example in PHP on that page.
The basic idea is that each method (that you want to be able to chain) of the class has to return $this -- which makes possible to call other methods of that same class on the returned $this.
And, of course, each method has access to the properties of the current instance of the class -- which means each method can "add some information" to the current instance.
Basically, you have to make every method in the class return the instance:
<?php
class Object_Evaluate{
private $x;
public function __construct($x){
$this->x = $x;
}
public function __toString(){
return 'condition is ' . $this->x;
}
}
class Foo{
public function select($what){
echo "I'm selecting $what\n";
return $this;
}
public function from($where){
echo "From $where\n";
return $this;
}
public function where($condition){
echo "Where $condition\n";
return $this;
}
public function limit($condition){
echo "Limited by $condition\n";
return $this;
}
public function order($order){
echo "Order by $order\n";
return $this;
}
}
$object = new Foo;
$object->select('something')
->from('table')
->where( new Object_Evaluate('x') )
->limit(1)
->order('x');
?>
This is often used as pure eye candy but I suppose it has its valid usages as well.
class c
{
function select(...)
{
...
return $this;
}
function from(...)
{
...
return $this;
}
...
}
$object = new c;

Categories