เคยสงสัยไหมว่า Object เรียก method เป็นทอดๆ ได้ยังไง (2009-09-11)
ผมคิดว่าน่าจะประมาณนี้นะครับ
<?php
Class MyObj{
function A(){
echo "AAAAAA ";
return $this;
}
function B(){
echo "BBBBBB ";
return $this;
}
function C(){
echo "CCCCCC ";
return $this;
}
}
class TestClass {
public $Obj;
function TestClass(){
$this->Obj = new MyObj();
}
}
$test = new TestClass();
$test->Obj->A()->B()->C();
?>
ถ้าเป็น C# ก็ประมาณนี้นะ
class Test
{
public Test A()
{
Console.WriteLine("A");
return this;
}
public Test B()
{
Console.WriteLine("B");
return this;
}
public Test C()
{
Console.WriteLine("C");
return this;
}
}
class Program
{
static void Main(string[] args)
{
Test test = new Test();
test.A().B().C();
Console.Read();
}
}
เป็นไงล่ะ เท่ห์ซะไม่มีผมว่า
|