php技术博客
让天下没有搞不定的bug~

关注:2k的php程序题公布答案了

         前几天发布了月薪2k的php程序题,查看原文问题请点击:原文试题 现在把答案贴在这里,你们答对了吗?

<?php

class p {

function a()

{

echo __FUNCTION__, ‘ ‘;

return $this;

}

function b()

{

echo __FUNCTION__, ‘ ‘;

return $this;

}

function init()

{

echo “end”;

}

}

$p = new p();

$p->a()->init();

echo “\n”;

$p->b()->init();

echo “\n”;

$p->a()->b()->init();

echo “\n”;

$p->b()->a()->init();

echo “\n”;

其实这道题考了面向对象的基本概念,return $this 就是返回这个对象,在PHP类里面$this关键字就是代表这个类内部的引用。如你上面所说的return $this;就是相当于把该对象返回到方法a() \b()中。

 

赞(0)
未经允许不得转载:PHP技术博客 » 关注:2k的php程序题公布答案了