PHP方法链接的最后一个对象
在使用方法链的PHP中,在链中调用最后一个方法之后,如何提供函数调用 在使用同一实例时(请参见下文)。这将扼杀实现析构函数的想法 最终结果是从定义的链属性(当然)返回私有“insert()”的值和函数调用,而不必公开调用它,无论顺序如何 注意,如果我回送(_toString)这些方法,它将检索最终生成的唯一代码,这是转换字符串的正常行为 下面的示例:PHP方法链接的最后一个对象,php,oop,methods,chaining,method-chaining,Php,Oop,Methods,Chaining,Method Chaining,在使用方法链的PHP中,在链中调用最后一个方法之后,如何提供函数调用 在使用同一实例时(请参见下文)。这将扼杀实现析构函数的想法 最终结果是从定义的链属性(当然)返回私有“insert()”的值和函数调用,而不必公开调用它,无论顺序如何 注意,如果我回送(_toString)这些方法,它将检索最终生成的唯一代码,这是转换字符串的正常行为 下面的示例: class object { private $data; function __construct($name) {
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
链1、2和3将根据方法中的所有值生成一个唯一的代码,并提供一个操作,例如自动插入数据库或创建文件(在本例中使用)
正如您所看到的,没有任何字符串设置、铸造或回音发生。我使用PeeHaa,这没有任何意义!:)
在使用最后一个链之后(无法展望未来),发生奇迹的唯一机会是析构函数/关闭函数或手动转换/调用insert()Im with PeeHaa,这毫无意义!:)
在使用最后一个链之后(无法展望未来),发生奇迹的唯一机会是析构函数/关闭函数或手动强制转换/调用insert()您可以保留需要初始化的事物列表,以及它们是否 在这种情况下是不是这样。然后每次使用时检查列表 初始化方法之一。比如:
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";
}
}
但就我个人而言,我认为这比它的价值更麻烦。更好的海事组织
要公开您的insert
方法,并让代码的用户知道
初始化完成。因此,应该使用以下方法:
$o->red(1)->green(2)->blue(0)->insert();
-更新-
如果不可能预测需要调用哪些函数
你真的需要把它说清楚。我看不出有什么办法可以解决这个问题。原因
是不是php真的无法区分
$o1 = new A();
$o2 = $o1->stuff();
及
在一种允许重载的语言中,我想这是可能的,但事实上
真的很混乱,通常不是一个好主意
可以移动显式部分,使其不在调用末尾
链子,但我不确定这是否会让你更快乐?这也会违反法律
您不想使用其他实例的愿望。它可能看起来像这样:
O类{
公共函数构造(InitO$ini){
//做事
回声“Whee\n”;
}
}
类初始化{
公共功能红色($n){
退还$this;
}
公共功能绿色(n美元){
退还$this;
}
公共功能蓝色($n){
退还$this;
}
}
$o=newo((newinito())->红色(10)->红色(9)->绿色(7));
当然,您可以通过使用其他包装方式仅使用一个实例
但是我现在能想到的唯一办法就是看起来更难看。你可以保留一份需要初始化的东西的清单,以及它们是否 在这种情况下是不是这样。然后每次使用时检查列表 初始化方法之一。比如:
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";
}
}
但就我个人而言,我认为这比它的价值更麻烦。更好的海事组织
要公开您的insert
方法,并让代码的用户知道
初始化完成。因此,应该使用以下方法:
$o->red(1)->green(2)->blue(0)->insert();
-更新-
如果不可能预测需要调用哪些函数
你真的需要把它说清楚。我看不出有什么办法可以解决这个问题。原因
是不是php真的无法区分
$o1 = new A();
$o2 = $o1->stuff();
及
在一种允许重载的语言中,我想这是可能的,但事实上
真的很混乱,通常不是一个好主意
可以移动显式部分,使其不在调用末尾
链子,但我不确定这是否会让你更快乐?这也会违反法律
您不想使用其他实例的愿望。它可能看起来像这样:
O类{
公共函数构造(InitO$ini){
//做事
回声“Whee\n”;
}
}
类初始化{
公共功能红色($n){
退还$this;
}
公共功能绿色(n美元){
退还$this;
}
公共功能蓝色($n){
退还$this;
}
}
$o=newo((newinito())->红色(10)->红色(9)->绿色(7));
当然,您可以通过使用其他包装方式仅使用一个实例
但是我现在能想到的唯一方法看起来要难看得多。您也可以决定在不使用对象的情况下静态实现这一点
<?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
?>
您也可以决定在不使用对象的情况下静态实现此功能
<?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
?>
好的,让我们看一个代码示例
<?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');
一旦我们触底,我们什么都可以叫。
*(注意,一旦我处理完一个变量,我就使用null,我来自我们自己管理内存的C系列)
希望它对您有所帮助。好的,让我们看一个代码示例
<?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');
一旦我们触底,我们什么都可以叫。
*(注意,一旦我处理完一个变量,我就使用null,我来自我们自己管理内存的C系列)
希望它能对您有所帮助。链1和2是什么/在哪里?您想在三次方法调用后运行一些东西吗?所有颜色都设置好之后?您想要另一个带有回调的方法吗?在PHP神奇地发现您何时想要运行代码之后?还有,析构函数是如何在这一切中发挥作用的?@AmalMurali将代码向下滚动到最后。@PeeHaa correct!最后一个回调方法,包含从三个方法调用中附加的所有数据。那么,是什么阻止您添加该方法?链1和2是什么/在哪里?要在三个方法调用之后运行一些内容吗?不管怎样,我们都要努力