PHP: Chaining Method Calls [duplicate] - php

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
PHP method chaining?
I occasionally see some php applications use classes like that:
$Obj = new Obj();
$Obj->selectFile('datafile.ext')->getDATA();
The example above gets the contents of the selected file then returns them ( just an example );
Well, before i decided to ask you how can I do this, I tried this:
class Someclass {
public function selectFile( $filename ) {
$callAclass = new AnotherClass( $filename );
return $callAclass;
}
// Other functions ...
}
class AnotherClass {
protected $file_name;
public function __construct ( $filename ) { $this->file_name = $filename; }
public function getDATA ( ) {
$data = file_get_contents( $this->file_name );
return $data;
}
// funcs , funcs, funcs ....
}
Is that the right way to accomplish this task? And please tell me what these classes are called.

It's called method chaining. Have a look at this question on SO.
Here's a way you can do what you're trying to achieve:
class File
{
protected $_fileName;
public function selectFile($filename)
{
$this->_fileName = $filename;
return $this;
}
public function getData()
{
return file_get_contents($this->_fileName);
}
}
$file = new File();
echo $file->selectFile('hello.txt')->getData();
Notice we return $this in the selectFile, this enables us to chain another method onto it.

The above example (the first one) is something called chaining. Here's a regular class:
class a_class
{
public function method_a()
{
echo 'method a';
}
public function method_b()
{
echo ' - method b';
}
}
You'd call those like this:
$class = new a_class();
$class->method_a();
$class->method_b();
Which would echo 'method a - method b'. Now to chain them, you'd return the object in the methods:
class a_class
{
public function method_a()
{
echo 'method a';
return $this;
}
public function method_b()
{
echo ' - method b';
return $this;
}
}
You'd call those like this:
$class = new a_class();
$class->method_a()->method_b();
Which would also echo 'method a - method b'.
Although I have returned the object in the last method - you strictly wouldn't need to, only the preceding methods require that for chaining.

Related

PHP Magic __invoke upon object-property of a class

Consider this class arrangement - and in particular the magic function __invoke:
class Barman {
public function __construct() {
// .. .constructor stuff - whatever
}
public function makeDrink() {
return "vodka martini, shaken";
}
}
class Bar {
private $arr_barmen = array();
public function __construct() {
$this->arr_barmen['john'] = new Barman();
}
public function __invoke($barman_id) {
echo "I have been invoked";
return $this->arr_barmen[$barman_id];
}
public function aBarFunc($param) {
return "yes it worked ," .$param;
}
}
class Foo {
public $myBar;
public function __construct() {
$this->myBar = new Bar();
}
}
I want to write syntax like this
$company = new Foo();
$company->myBar('john')->makeDrink();
Preferred result:
"vodka martini, shaken"
Actual result:
"Call to undefined method Foo::myBar()"
Invoking myBar() with the magic method should return a barman Object upon which you can call any of the barman's public methods
But now consider this (which does work)
$company = new Foo();
$myBar = $company->myBar;
$drink = $myBar('john')->makeDrink();
echo $drink;
// Result:
// I have been invoked
// vodka martini, shaken
So what's going on? I don't like that workaround - it's not sleek.
I need it to work this way:
$company->myBar('john')->makeDrink();
Please help? :-)
I believe you can just add braces around it:
$company = new Foo();
$drink = ($company->myBar)('john')->makeDrink();
echo $drink; // vodka martini, shaken
This is being caused by an ambiguity in the call you're trying to make:
$company->myBar('john')->makeDrink();
Because myBar is a property, the PHP interpreter isn't expecting it to be callable. It is parsing it as an attempt to call a method called myBar() which doesn't exist, and is thus throwing the error.
The direct way to resolve this is to clarify the ambiguity for the interpreter. You do this by adding curly braces around the property, as follows:
$company->{myBar}('john')->makeDrink();
The code is now explicit that myBar is a property and should be accessed as such, but that it contains a value that is callable and that you wish to make that call.
This whole topic is complicated (slightly) by the fact that PHP 5.x and PHP 7.x behave differently with regard to how they default to handling these kinds of ambiguity. PHP 7 changed the defaults in order to correct some internal inconsistencies within the language. The result is that in situations like this where you have an ambiguity, if you want your code to work across both PHP 5.x and 7.x, you should always use the braces to explicitly define how you want it to work, regardless of whether your code works for you without them.
There is some documentation about this change in the PHP 7.0 upgrade notes, although the examples given don't cover your exact situation.
To attain chaining you have to return the Barman object in invoke.
class Barman {
public function __construct() {
// .. .constructor stuff - whatever
}
public function makeDrink() {
return "vodka martini, shaken";
}
}
class Bar {
private $arr_barmen = array();
public function __construct() {
$this->arr_barmen['john'] = new Barman();
}
public function __invoke($barman_id) {
echo "I have been invoked";
return $this->arr_barmen[$barman_id] = new Barman();
}
public function aBarFunc($param) {
return "yes it worked ," . $param;
}
}
class Foo {
public $myBar;
public function __construct() {
$this->myBar = new Bar();
}
// create a function with variable name to invoke the object
public function myBar($name) {
$mybar = $this->myBar;
return $mybar($name);
}
}
Thank you for the responses. I devised a cute workaround as follows:
class Barman {
public function __construct() {
}
public function makeDrink() {
return "vodka martini, shaken";
}
}
class Bar {
private $arr_barmen = array();
public function __construct() {
$this->arr_barmen['john'] = new Barman();
}
public function getBarman($barman_id) {
return $this->arr_barmen[$barman_id];
}
public function __invoke($barman_id) {
echo "I have been invoked \n";
return $this->arr_barmen[$barman_id];
}
}
class Foo {
private $_myBar;
public function __construct() {
$this->_myBar = new Bar();
}
// The fix
public function myBar($barman_id) {
return $this->_myBar->getBarman($barman_id);
}
}
Usage:
$company = new Foo();
$drink = $company->myBar('john')->makeDrink();
echo $drink; // vodka martini, shaken
How it works?
Foo->myBar becomes private (Foo->$_myBar);
we create a public function with the name myBar inside Foo;
we create a "getBarman" fuction inside Bar which is called from Foo->myBar('john')
A few more steps - and now there's no ambiguity - Foo->myBar() IS always a function.
Cheers
M

Return variable class name from php factory

I've got a factory that I want to return a ::class from. However, the factory could potentially return a couple dozen different types (determined by the type of object passed into the factory), named TypeOneObject, TypeTwoObject etc. Is it possible to return the class using a variable, something like this?
$type = $myrequiredclass->getType();
return $type."Object"::class; // wanting TypeOneObject::class
It seems like no matter how I construct this return statement I always get PHP Parse error: syntax error, unexpected '::'
I know it'd be easy enough to do with a big if/then or switch but I'd like to avoid that.
Here's a more fleshed out scenario:
class TypeOneObject
{
public static function whoAreYou()
{
return 'Type One Object!';
}
}
class MyRequiredClass
{
public function getType()
{
return 'TypeOne';
}
}
class MyFactory
{
public static function getFactoryObject(MyRequiredClass $class)
{
$type = $class->getType()."Object";
return $type::class;
}
}
$object = MyFactory::getFactoryObject(new MyRequiredClass());
$object::whoAreYou();
The best way to get the class name from the $type instance is to use php get_class_methods function. This will get us all the methods inside the class instance. from there we can filter and use call_user_func to call the method and get the right values.
class TypeOneObject
{
public static function whoAreYou()
{
return 'Type One Object!';
}
}
class MyRequiredClass
{
public function getType()
{
return 'TypeOne';
}
}
class MyFactory
{
public static function getFactoryObject(MyRequiredClass $class)
{
$methods = get_class_methods($class);
$method = array_filter($methods, function($method) {
return $method == 'getType';
});
$class = new $class();
$method = $method[0];
$methodName = call_user_func([$class, $method]);
$objectName = sprintf('%sObject', $methodName);
return new $objectName;
}
}
$object = MyFactory::getFactoryObject(new MyRequiredClass());
echo $object::whoAreYou();
Output
Type One Object!

PHP Last Object of Method Chaining

In PHP using method chaining how would one go about supplying a functional call after the last method being called in the chain?
Also while using the same instance (see below). This would kill the idea of implementing a destructor.
The end result is a return value and functional call of private "insert()" from the defined chain properties (of course) without having to call it publicly, no matter of the order.
Note, if I echo (__toString) the methods together it would retrieve the final generated unique code which is normal behavior of casting a string.
Example below:
class object
{
private $data;
function __construct($name) {
// ... some other code stuff
}
private function fc($num) {
// some wicked code here
}
public function green($num) {
$this->data .= fc($num*10);
return $this;
}
public function red($num) {
$this->data .= fc($num*25);
return $this;
}
public function blue($num) {
$this->data .= fc($num*1);
return $this;
}
// how to get this baby to fire ?
private function insert() {
// inserting
file_put_content('test_code.txt', $this->data);
}
}
$tss = new object('index_elements');
$tss->blue(100)->green(200)->red(100); // chain 1
$tss->green(0)->red(100)->blue(0); // chain 2
$tss->blue(10)->red(80)->blue(10)->green(0); // chain 3
Chain 1, 2, and 3 would generated an unique code given all the values from the methods and supply an action, e.g. automatically inserting in DB or creating a file (used in this example).
As you can see no string setting or casting or echoing is taking place.
You could keep a list of things that needs to be initialised and whether they
have been so in this instance or not. Then check the list each time you use
one of the initialisation methods. Something like:
class O {
private $init = array
( 'red' => false
, 'green' => false
, 'blue' => false
);
private function isInit() {
$fin = true;
foreach($this->init as $in) {
$fin = $fin && $in;
}
return $fin;
}
public function green($n) {
$this->init['green'] = true;
if($this->isInit()) {
$this->insert();
}
}
public function red($n) {
$this->init['red'] = true;
if($this->isInit()) {
$this->insert();
}
}
public function blue($n) {
$this->init['blue'] = true;
if($this->isInit()) {
$this->insert();
}
}
private function insert() {
echo "whee\n";
}
}
But personally I think this would be more hassle then it's worth. Better imo
to expose your insert method and let the user of you code tell when the
initialisation is finished. So something that should be used like:
$o->red(1)->green(2)->blue(0)->insert();
-update-
If it's the case that it's impossible to predict what functions need to be called
you really do need to be explicit about it. I can't see a way around that. The reason
is that php really can't tell the difference between
$o1 = new A();
$o2 = $o1->stuff();
and
$o2 = (new A())->stuff();
In a language that allows overloading = I guess it would be possible but really
really confusing and generally not a good idea.
It is possible to move the explicit part so that it's not at the end of the call
chain, but I'm not sure if that would make you happier? It would also go against
your desire to not use another instance. It could look something like this:
class O {
public function __construct(InitO $ini) {
// Do stuff
echo "Whee\n";
}
}
class InitO {
public function red($n) {
return $this;
}
public function green($n) {
return $this;
}
public function blue($n) {
return $this;
}
}
$o = new O((new InitO())->red(10)->red(9)->green(7));
You can of course use just one instance by using some other way of wrapping
but the only ways I can think of right now would look a lot uglier.
Im with PeeHaa, this makes no sense! :)
Only chance to have something magically happen after the last chain was used (without being able to look into the future) is a Destructor/Shutdown function OR a manually cast/call to insert()
You can also decide to implement this statically without using objects.
<?php
class Object
{
private static $data;
public static function set($name)
{
// ... some other code stuff
}
private static function fc($num)
{
// some wicked code here
}
public static function green($num)
{
self::$data .= self::fc($num*10);
return new static;
}
public static function red($num)
{
self::$data .= self::fc($num*25);
return new static;
}
public static function blue($num) {
self::$data .= self::fc($num*1);
return new static;
}
// how to get this baby to fire ?
public static function insert()
{
// inserting
file_put_content('test_code.txt', self::$data);
}
}
//$tss = new object('index_elements');
$Object::set('index_elements')->blue(100)->green(200)->red(100)->insert(); // chain 1
$Object::set('index_elements')->green(0)->red(100)->blue(0)->insert(); // chain 2
$Object::set('index_elements')->blue(10)->red(80)->blue(10)->green(0)->insert(); // chain 3
?>
Ok let's see a code example
<?php
// map dummy class
class map
{
// __call magic method
public function __call($name, $args)
{
return $this;
}
}
// now we chain
$map = new map;
// let's find me
$map->start('here')
->go('right')
->then()
->turn('left')
->and('get water')
->dontEat()
->keep('going')
->youShouldSeeMe('smiling');
here we don't know what the last method would be and we need to trigger a kinda operation or event once we hit the end.
According to data structure we can call this the LIFO stack. (Last in first out)
so how did i solve this on PHP?
// i did some back tracing
... back to the __call function
function __call($name, $args)
{
$trace = debug_backtrace()[0];
$line = $trace['line'];
$file = $trace['file'];
$trace = null;
$getFile = file($file);
$file = null;
$getLine = trim($getFile[$line-1]);
$line = null;
$getFile = null;
$split = preg_split("/(->)($name)/", $getLine);
$getLine = null;
if (!preg_match('/[)](->)(\S)/', $split[1]) && preg_match('/[;]$/', $split[1]))
{
// last method called.
var_dump($name); // outputs: youShouldSeeMe
}
$split = null;
return $this;
}
And whoolla we can call anything once we hit the bottom.
*(Notice i use null once i am done with a variable, i come from C family where we manage memory ourselves)
Hope it helps you one way or the other.

PHP dynamic class extending

I know you can extend a class when constructing it like the following:
class b extends a {
}
But is it possible to dynamically extend classes from the scripts? Such as:
$b = new b($input) extends a;
What I wish to accomplish is to extend the module differnetly wheither it's used in admin rather than the public pages. I know I can create two different parent classes by the same name and only include one per admin or public. But my question is, is it possible to do it dynamically in PHP?
No, not without an extension like RunKit.
You might consider an alternative approach. If you want B to assume the functionality of A, perhaps something like the following could provide a sort of "mixin" approach. The general picture is that, instead of B being a child of A, B delegates to A.
<?php
class MixMeIn
{
public $favouriteNumber = 7;
public function sayHi() {
echo "Hello\n";
}
}
class BoringClass
{
private $mixins = array();
public function mixin($object)
{
$this->mixins[] = $object;
}
public function doNothing() {
echo "Zzz\n";
}
public function __call($method, $args)
{
foreach ($this->mixins as $mixin)
{
if (method_exists($mixin, $method))
{
return call_user_func_array(array($mixin, $method), $args);
}
}
throw new Exception(__CLASS__ + " has no method " + $method);
}
public function __get($attr)
{
foreach ($this->mixins as $mixin)
{
if (property_exists($mixin, $attr))
{
return $mixin->$attr;
}
}
throw new Exception(__CLASS__ + " has no property " + $attr);
}
public function __set($attr, $value)
{
foreach ($this->mixins as $mixin)
{
if (property_exists($mixin, $attr))
{
return $mixin->$attr = $value;
}
}
throw new Exception(__CLASS__ + " has no property " + $attr);
}
}
// testing
$boring = new BoringClass();
$boring->doNothing();
try {
$boring->sayHi(); // not available :-(
}
catch (Exception $e) {
echo "sayHi didn't work: ", $e->getMessage(), "\n";
}
// now we mixin the fun stuff!
$boring->mixin(new MixMeIn());
$boring->sayHi(); // works! :-)
echo $boring->favouriteNumber;
Just a zany idea. I hope I understood the question correctly.
You can't, but this has been requested for a few years: https://bugs.php.net/bug.php?id=41856&edit=1
You can define the classes within an eval, but it's more trouble than declaring the class normally.
But you cannot use extends while object creation. extends is used in class definition only and defines which other class is "parent" for our new class.
Alternatively, if you are comfortable with javascript-style inheritance and don't mind losing typechecking:
<? //PHP 5.4+
final class ExpandoLookalike {
//Allow callable properties to be executed
public function __call($name, $arguments) {
\call_user_func_array($this->$name, $arguments);
}
}
$newBaseModule = static function(){
$base = new ExpandoLookalike();
//Common base functions get assigned here.
$basePrivateVar = 42;
$base->commonFunction = static function($params1, $params2) use ($basePrivateVar){
echo "common function\n";
};
$base->comment = static function() use ($basePrivateVar){
echo "Doing base comment with $basePrivateVar\n";
};
return $base;
};
//Javascript-style extends
$newAdminModule = static function($param) use ($newBaseModule){
$base = $newBaseModule();
$privateVar = 5;
$base->adminProperty = 60;
$base->suspendSite = static function() use ($param, $privateVar){
echo 'Doing admin only function ';
echo "with $param, $privateVar\n";
};
return $base;
};
$newPublicModule = static function() use ($newBaseModule){
$base = $newBaseModule();
$privateVar = 3;
//Javascript-style overloading
$oldComment = $base->comment;
$base->comment = static function($data) use ($oldComment, $privateVar){
$oldComment();
echo 'Doing public function ';
echo "with $data\n";
};
return $base;
};
$baseModule = $newBaseModule();
$adminModule = $newAdminModule('P');
$publicModule = $newPublicModule();
$adminModule->suspendSite(); //echos 'Doing admin only function with P, 5'
echo "{$adminModule->adminProperty}\n"; //echos '60'
$publicModule->comment('com'); //echos 'Doing base comment with 42'
//'Doing public function with com'
?>
Despite closing the door to traits and interfaces, it opens up other interesting doors to compensate:
<? //PHP 5.4+
$inheritAllTheThings = static function(){
$base = new ExpandoLookalike();
foreach(\func_get_args() as $object){
foreach($object as $key => $value){
//Properties from later objects overwrite properties from earlier ones.
$base->$key = $value;
}
}
return $base;
};
$allOfEm = $inheritAllTheThings(
$newPublicModule(),
$newAdminModule('Q'),
['anotherProp' => 69,]
);
$allOfEm->comment('f'); //echos 'Doing base comment with 42'
//Because AdminModule came after PublicModule, the function that echos 'f'
//from PublicModule was overridden by the function from AdminModule.
//Hence, order denotes resolutions for multiple inheritance collisions.
$allOfEm->suspendSite(); //echos 'Doing admin only function with Q, 5'
echo $allOfEm->anotherProp . "\n"; //echos '69'
?>
You can with typecasting. If a extends b then you could do
$a=(a)(new b($input));
Which isn't exactly the same, but similar.
You can look: https://github.com/ptrofimov/jslikeobject
Author implemented JS-like objects with support of inheritance.
But perhaps it is not so good to use such objects instead of usual ones.
Yes, as cory mentioned, this feature has been requested before. But before that, you can create a workaround. Here is my old school trick for this
Create two separate classes like these:
class a {
}
class b {
public $object;
}
Then, create an extended version too
class bextendeda extends a {
}
In the constructor method of class b, place few functions which redirects to the extended object if requested.
class b {
public $object;
public function __contruct($extend = false) {
if($extend) $this -> object = new bextendeda();
else $this -> object = $this;
}
function __get($prop) {
return $this-> object -> $prop;
}
function __set($prop, $val) {
$this-> object -> $prop = $val;
}
function __call($name, $arguments)
{
return call_user_func_array(array($this -> object, $name), $arguments);
}
}
And there you have it, IF you want the extended version just do this
$b = new b(true);
If not
$b = new b();
Enjoy :)

Wildcards in PHP functions

I am not sure if the term "Wildcard" can explain my point, but sometimes in some ready scripts we are able to call a non defined function like find_by_age(23) where age can be anything else that's mapped to a database table record. So i can call find_by_name, find_by_email, find_by_id and so on. So how can we do such thing either in procedural or object oriented ways ?
The term you are looking for is magic method.
Basically like this:
class Foo {
public function __call($method,$args) {
echo "You were looking for the method $method.\n";
}
}
$foo = new Foo();
$foo->bar(); // prints "You were looking for the method bar."
For what you are looking for, you just filter out bad function calls and redirect good ones:
class Model {
public function find_by_field_name($field,$value) { ... }
public function __call($method,$args) {
if (substr($method,0,8) === 'find_by_') {
$fn = array($this,'find_by_field_name');
$arguments = array_merge(array(substr($method,8)),$args);
return call_user_func_array($fn,$arguments);
} else {
throw new Exception("Method not found");
}
}
}
You can use them by defining a __call magic method in your class, you can use them only in classes. on global scope
Quoting from PHP Manual:
<?php
class MethodTest {
public function __call($name, $arguments) {
// Note: value of $name is case sensitive.
echo "Calling object method '$name' "
. implode(', ', $arguments). "\n";
}
/** As of PHP 5.3.0 */
public static function __callStatic($name, $arguments) {
// Note: value of $name is case sensitive.
echo "Calling static method '$name' "
. implode(', ', $arguments). "\n";
}
}
$obj = new MethodTest;
$obj->runTest('in object context');
MethodTest::runTest('in static context'); // As of PHP 5.3.0
?>
The above example will output:
Calling object method 'runTest' in object context
Calling static method 'runTest' in static context
For a procedural solution you can simply use string concatenation to get the job done. You can also fancy things up a bit by calling it an implementation of the strategy pattern.
<?php
/**
* Employ a find by name strategy
*/
function find_by_name($name)
{
echo "You are searching for users with the name $name";
return array();
}
/**
* Employ a find by age strategy
*/
function find_by_age($age)
{
echo "You are searching for users who are $age years old";
return array();
}
/**
* Find users by using a particular strategy
*/
function find_using_strategy($strategy='age', $parameter)
{
$results = array();
$search_function = 'find_by_' . $search_field;
if (function_exists($search_function)) {
$results = $search_function($parameter);
}
return $results;
}
$users = find_using_strategy('name', 'Matthew Purdon');
var_dump($users);

Categories