4,250
社区成员
发帖
与我相关
我的任务
分享class B // .... 略
class A extents B
{
function __construct()
{
if( $a > 1 ) //这里如何跳出, 或终止执行, 不能用exit
}
function a()
{
echo 'this is a.';
}
function b()
{
echo 'this is b.';
}
}
//要执行的代码是这样
$test = new A;
$test->a(); // 假设执行这方法时, 构造函数里$a>1, 则跳出不执行, $a的判断只能发生在构造函数内
<?php
class A
{
protected $show_a = true;
function __construct($a = 0)
{
$this->show_a = !($a > 1);
}
function aa()
{
if($this->show_a) echo 'this is a.';
}
function bb()
{
echo 'this is b.';
}
}
echo '11';
$test = new A;
$test->aa();
echo '<br/>22';
$test = new A(3.2);
$test->aa();
echo '<br/>33';
$test = new A(0.5);
$test->aa();
?>
<?php
class A
{
function __construct($what = 'aa')
{
($what != 'aa') ? $this->bb() : $this->aa();//二选一之一
method_exists($this,$what) ? $this->$what() : print('no A::'.$what.'()!');//二选一之二
}
function aa()
{
echo 'this is a.';
}
function bb()
{
echo 'this is b.';
}
}
$test = new A;
echo '<br/>';
$test = new A('bb');
echo '<br/>';
$test = new A('cc');
?>