Wil*_*lco 49 php oop method-chaining
是否可以使用静态类将静态方法链接在一起?说我想做这样的事情:
$value = TestClass::toValue(5)::add(3)::subtract(2)::add(8)::result();
Run Code Online (Sandbox Code Playgroud)
...显然我希望$ value被分配数字14.这可能吗?
更新:它不起作用(你不能返回"自我" - 它不是一个实例!),但这是我的想法带我的地方:
class TestClass {
public static $currentValue;
public static function toValue($value) {
self::$currentValue = $value;
}
public static function add($value) {
self::$currentValue = self::$currentValue + $value;
return self;
}
public static function subtract($value) {
self::$currentValue = self::$currentValue - $value;
return self;
}
public static function result() {
return self::$value;
}
}
Run Code Online (Sandbox Code Playgroud)
在完成这项工作之后,我认为简单地使用类实例而不是尝试链接静态函数调用(这看起来不可能,除非上面的示例可以某种方式进行调整)更有意义.
Mat*_*rne 48
我喜欢Camilo上面提供的解决方案,主要是因为您所做的只是改变静态成员的值,并且因为您确实想要链接(即使它只是合成糖),那么实例化TestClass可能是最好的方法. .
如果你想限制类的实例化,我建议使用Singleton模式:
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();
Run Code Online (Sandbox Code Playgroud)
Ari*_*lam 40
class oop{
public static $val;
public static function add($var){
static::$val+=$var;
return new static;
}
public static function sub($var){
static::$val-=$var;
return new static;
}
public static function out(){
return static::$val;
}
public static function init($var){
static::$val=$var;
return new static;
}
}
echo oop::init(5)->add(2)->out();
Run Code Online (Sandbox Code Playgroud)
sec*_*tus 31
关于php5.3的小疯狂代码......只是为了好玩.
namespace chaining;
class chain
{
static public function one()
{return get_called_class();}
static public function two()
{return get_called_class();}
}
${${${${chain::one()} = chain::two()}::one()}::two()}::one();
Run Code Online (Sandbox Code Playgroud)
sec*_*tus 12
使用php7,您将能够使用所需的语法,因为新的统一变量语法
<?php
abstract class TestClass {
public static $currentValue;
public static function toValue($value) {
self::$currentValue = $value;
return __CLASS__;
}
public static function add($value) {
self::$currentValue = self::$currentValue + $value;
return __CLASS__;
}
public static function subtract($value) {
self::$currentValue = self::$currentValue - $value;
return __CLASS__;
}
public static function result() {
return self::$currentValue;
}
}
$value = TestClass::toValue(5)::add(3)::subtract(2)::add(8)::result();
echo $value;
Run Code Online (Sandbox Code Playgroud)
如果toValue(x)返回一个对象,你可以这样做:
$value = TestClass::toValue(5)->add(3)->substract(2)->add(8);
Run Code Online (Sandbox Code Playgroud)
假设toValue返回对象的新实例,并且每个next方法都会对其进行变更,返回$ this的实例.
这更准确、更容易、更易于阅读(允许代码完成)
class Calculator
{
public static $value = 0;
protected static $onlyInstance;
protected function __construct ()
{
// disable creation of public instances
}
protected static function getself()
{
if (static::$onlyInstance === null)
{
static::$onlyInstance = new Calculator;
}
return static::$onlyInstance;
}
/**
* add to value
* @param numeric $num
* @return \Calculator
*/
public static function add($num)
{
static::$value += $num;
return static::getself();
}
/**
* substruct
* @param string $num
* @return \Calculator
*/
public static function subtract($num)
{
static::$value -= $num;
return static::getself();
}
/**
* multiple by
* @param string $num
* @return \Calculator
*/
public static function multiple($num)
{
static::$value *= $num;
return static::getself();
}
/**
* devide by
* @param string $num
* @return \Calculator
*/
public static function devide($num)
{
static::$value /= $num;
return static::getself();
}
public static function result()
{
return static::$value;
}
}
Run Code Online (Sandbox Code Playgroud)
例子:
echo Calculator::add(5)
->subtract(2)
->multiple(2.1)
->devide(10)
->result();
Run Code Online (Sandbox Code Playgroud)
结果:0.63
人们疯狂地把这件事复杂化了。
看一下这个:
class OopClass
{
public $first;
public $second;
public $third;
public static function make($first)
{
return new OopClass($first);
}
public function __construct($first)
{
$this->first = $first;
}
public function second($second)
{
$this->second = $second;
return $this;
}
public function third($third)
{
$this->third = $third;
return $this;
}
}
Run Code Online (Sandbox Code Playgroud)
用法:
OopClass::make('Hello')->second('To')->third('World');
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
23128 次 |
最近记录: |