dpa84373 2012-04-04 08:33
浏览 63

在PHP上如何从父类中的变量中获取值?

class config {
    public $pageName;

    function __construct($pageName=''){
        $this->pageName = $pageName;        
    }
}


class header extends config {
    function display(){
        echo parent::$this->pageName;
    }

}


$config = new config('Home Page');
$header = new header();
$header->display();

This doesn't display anything, I thought it should have displayed 'Home Page'.

Any idea how i can achieve this?

  • 写回答

4条回答 默认 最新

  • douchi0638 2012-04-04 08:37
    关注

    The $header object has no relationship to the $config object. Just because their class hierarchy is connected doesn't mean that the object instances share data.

    $config1 = new config('Home Page');
    $config2 = new config();
    

    Here $config2 couldn't access the value 'Home Page' either, because it's a different object. It's not a matter of class hierarchy.

    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分