Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP类逻辑_Php_Oop_Class_Abstract Class - Fatal编程技术网

PHP类逻辑

PHP类逻辑,php,oop,class,abstract-class,Php,Oop,Class,Abstract Class,我的问题很简单,因为: class MyClass{ function a(){ echo "F.A "; } function b(){ echo "F.B "; } } $c=new MyClass; $c->a()->b()->b()->a(); 以便输出: F.A F.B F.B F.A 需要对代码进行哪些更改才能使其工作,或者它应该按原样工作,甚至只是它所称的那样工作。如果我能得到这个术语的名称,我可以在m

我的问题很简单,因为:

class MyClass{
   function a(){
       echo "F.A ";
   }
   function b(){
       echo "F.B ";
   }
}

$c=new MyClass;
$c->a()->b()->b()->a();
以便输出:

F.A F.B F.B F.A

需要对代码进行哪些更改才能使其工作,或者它应该按原样工作,甚至只是它所称的那样工作。如果我能得到这个术语的名称,我可以在mysqlf上研究它,但我不太确定谷歌应该做什么


提前谢谢

在每个功能中,您必须:

  return $this;

在每个功能中,您必须:

  return $this;

将这样的方法串在一起称为链接

退还$this;在每个方法中,都将启用可链接性,因为它会不断地将实例从一个方法传递到另一个方法,从而维护链

你必须明确地这样做,因为

所以,你只需要再多两行

<?php
    class MyClass{
   function a(){
       echo "F.A ";
       return $this; // <== Allows chainability
   }
   function b(){
       echo "F.B ";
       return $this;
   }
}

$c=new MyClass;
$c->a()->b()->b()->a();
?>
请看一看,进一步探索PHP中的可链接性

你可以用链接能力做各种事情。方法通常涉及参数。下面是一个参数链:

<?php
   class MyClass{
   private $args = array();
   public function a(){
       $this->args = array_merge($this->args, func_get_args());
       return $this;
   }
   public function b(){
       $this->args = array_merge($this->args, func_get_args());
       return $this;
   }
   public function c(){
       $this->args = array_merge($this->args, func_get_args());
       echo "<pre>";
       print_r($this->args);
       echo "</pre>";       
       return $this;
   }   
}

$c=new MyClass;
$c->a("a")->b("b","c")->b(4, "cat")->a("dog", 5)->c("end")->b("no")->c("ok");

// Output:
//   Array ( [0] => a [1] => b [2] => c [3] => 4 [4] => cat 
//           [5] => dog [6] => 5 [7] => end )
//   Array ( [0] => a [1] => b [2] => c [3] => 4 [4] => cat 
//           [5] => dog [6] => 5 [7] => end [8] => no [9] => ok )
?>

将这样的方法串在一起称为链接

退还$this;在每个方法中,都将启用可链接性,因为它会不断地将实例从一个方法传递到另一个方法,从而维护链

你必须明确地这样做,因为

所以,你只需要再多两行

<?php
    class MyClass{
   function a(){
       echo "F.A ";
       return $this; // <== Allows chainability
   }
   function b(){
       echo "F.B ";
       return $this;
   }
}

$c=new MyClass;
$c->a()->b()->b()->a();
?>
请看一看,进一步探索PHP中的可链接性

你可以用链接能力做各种事情。方法通常涉及参数。下面是一个参数链:

<?php
   class MyClass{
   private $args = array();
   public function a(){
       $this->args = array_merge($this->args, func_get_args());
       return $this;
   }
   public function b(){
       $this->args = array_merge($this->args, func_get_args());
       return $this;
   }
   public function c(){
       $this->args = array_merge($this->args, func_get_args());
       echo "<pre>";
       print_r($this->args);
       echo "</pre>";       
       return $this;
   }   
}

$c=new MyClass;
$c->a("a")->b("b","c")->b(4, "cat")->a("dog", 5)->c("end")->b("no")->c("ok");

// Output:
//   Array ( [0] => a [1] => b [2] => c [3] => 4 [4] => cat 
//           [5] => dog [6] => 5 [7] => end )
//   Array ( [0] => a [1] => b [2] => c [3] => 4 [4] => cat 
//           [5] => dog [6] => 5 [7] => end [8] => no [9] => ok )
?>

方法链接在特定领域的语言中大量使用,尤其是由Martin Fowler发明的所谓的fluent接口。如果你想探索这种富有表现力的编程风格,可以在线查看他的DSL书籍的预印本

方法链接在特定领域的语言中大量使用,尤其是马丁·福勒(Martin Fowler)创造的所谓的fluent接口。如果你想探索这种富有表现力的编程风格,可以在线查看他的DSL书籍的预印本

Returnin$这将允许你在连锁效应中进行对象行,+1也可以接受这个答案。谢谢你们!这正是我要找的!Returnin$这将允许你在连锁效应中进行对象行,+1也可以接受这个答案。谢谢你们!这正是我要找的!谢谢你们两位!这正是我要找的!谢谢你们两位!这正是我要找的!