Chaining Static Methods in PHP

Chaining Static Methods in PHP?

I like the solution provided by Camilo above, essentially since all you're doing is altering the value of a static member, and since you do want chaining (even though it's only syntatic sugar), then instantiating TestClass is probably the best way to go.

I'd suggest a Singleton pattern if you want to restrict instantiation of the class:

class TestClass
{
public static $currentValue;

private static $_instance = null;

private function __construct () { }

public static function getInstance ()
{
if (self::$_instance === null) {
self::$_instance = new self;
}

return self::$_instance;
}

public function toValue($value) {
self::$currentValue = $value;
return $this;
}

public function add($value) {
self::$currentValue = self::$currentValue + $value;
return $this;
}

public function subtract($value) {
self::$currentValue = self::$currentValue - $value;
return $this;
}

public function result() {
return self::$currentValue;
}
}

// Example Usage:
$result = TestClass::getInstance ()
->toValue(5)
->add(3)
->subtract(2)
->add(8)
->result();

Is it possible to chain static together with non-static method in PHP?

I dont know what is the logic behind doing this, but still this solution will be helpful.

Try this code snippet here

<?php

namespace App\Classic;

ini_set('display_errors', 1);

class User
{

public $username;
public static $upassword;
public static $currentObject=null;//added this variable which hold current class object
public $age;
public $message;

public function __construct()//added a constructor which set's current class object in a static variable
{
self::$currentObject= $this;
}
public function username($username)
{
$this->username = $username;
echo $this->username . "<br>";
return $this;//added this statment which will return current class object
}

public static function password($upassword)
{
self::$upassword = $upassword;
echo self::$upassword . "<br>";
return self::$currentObject;
}

public function age($age)
{
$this->age = $age;
echo $this->age . "<br>";
return $this;
}

public function message($message)
{
$this->message = $message;
echo $this->message . "<br>";
return $this;
}

}

$user = new User();
$user::password('secret')
->username('admin')
->age(40)
->message('lorem ipsum');

return data with chain static method

I think you want


class Input
{
private static $data;

public static function set($input)
{
self::$data = $input;
return self;
}

public static function get()
{
echo self::$data.' - get method';
}
}

Input::set('ahmed')->get(); // ahmed - get method

but this you can use only once better is set name for value


class Input
{
private static $data = array();

public static function set($name, $input)
{
self::$data[$name] = $input;
return self;
}

public static function get($name)
{
echo self::$data[$name].' - get method';
}
}

Input::set('name', 'ahmed')->get('name'); // ahmed - get method

PHP OOP: Method Chaining

You cannot use Method Chaining with static methods because you cannot return a class level scope (return self won't do). Change your methods to regular methods and return $this in each method you want to allow chaining from.

Notice that you should not use T_PAAMAYIM_NEKUDOTAYIM to access instance methods as it will raise an E_STRICT Notice. Use T_OBJECT_OPERATOR for calling instance methods.

Also see:

  • Chaining Static Methods in PHP?

Chain methods ability without force to new keyword using php

Really all you need is for the static firstName methods to create a new instance of the class and return it.

The other setters just need to return $this to provide what's referred to as a fluent interface.

If the only way to create an instance is via the static firstName method, you'll also want to add a private / protected constructor.

For example

class Person
{
private $firstName;
private $lastName;
private $age;
private $father;

private function __construct(string $firstName) {
$this->firstName = $firstName;
}

public static function firstName(string $name) {
return new Person($name);
}

public function lastName(string $lastName) {
$this->lastName = $lastName;
return $this;
}

public function age(int $age) {
$this->age = $age;
return $this;
}

public function setFather(Father $father) {
$this->father = $father;
return $this;
}

public function toArray() {
// just an example
return [
'firstName' => $this->firstName,
'lastName' => $this->lastName,
'age' => $this->age,
'father' => $this->father->toArray(),
];
}
}

I would strongly advise against keeping the $name property as static. You don't want to change one instance's $name and have it change all others. This is why I've changed it to private $firstName in my example above.

PHP method chaining or fluent interface?

It's rather simple, really. You have a series of mutator methods that all return the original (or other) object. That way, you can keep calling methods on the returned object.

<?php
class fakeString
{
private $str;
function __construct()
{
$this->str = "";
}

function addA()
{
$this->str .= "a";
return $this;
}

function addB()
{
$this->str .= "b";
return $this;
}

function getStr()
{
return $this->str;
}
}

$a = new fakeString();

echo $a->addA()->addB()->getStr();

This outputs "ab"

Try it online!



Related Topics



Leave a reply



Submit