我需要创建一个带有父子类的变量。例子:
家长班
<?php
class parentClass
{
function __construct()
{
$subClass = new subClass();
$subClass->newVariable = true;
call_user_func_array( array( $subClass , 'now' ) , array() );
}
}
?>
子类
<?php
class subClass extends parentClass
{
public function now()
{
if( $this->newVariable )
{
echo "Feel Good!!!";
}else{
echo "Feel Bad!!";
}
echo false;
}
}
?>
执行父类
<?php
$parentClass = new parentClass();
?>
目前
注意:未定义的属性:第 6 行 subclass.php 中的 subClass::$newVariable
我真的需要这个:
感觉不错!!!
解决方案:
<?php
class parentClass
{
public $newVariable = false;
function __construct()
{
$subClass = new subClass();
$subClass->newVariable = true;
call_user_func_array( array( $subClass , 'now' ) , array() );
}
}
?>
<?php
class subClass extends parentClass
{
public function now()
{
if( $this->newVariable )
{
echo "Feel Good!!!";
}else{
echo "Feel Bad!!";
}
echo false;
}
}
?>