dtoqa66028 2012-06-20 11:22
浏览 38
已采纳

PHP从父类CLOSED访问变量

I've seen a few questions with really similar titles but they where irrelevant to my specific problem.

Basically, I want to access the variables from my core class in a class which extends core, but things seem to be quite complicated compared to other examples. I am using a MVC framework. I've simplified the code below to remove anything that was irrelevant.

index.php

// Load the core
include_once('core.php');
$core = new Core($uri, $curpath);
$core->loadController('property');

core.php

class Core
{
    public $uri;
    public $curpath;

    function __construct($uri, $curpath)
    {       
        $this->uri = $uri;
        $this->curpath = $curpath;
    }


    // Load the controller based on the URL
    function loadController($name)
    {       
        //Instantiate the controller
        require_once('controller/'.$name.'.php');
        $controller = new $name();
    }


}

property.php

class Property extends Core
{
    function __construct()
    {
        print $this->curpath;
    }   
}

Printing $this->curpath just returns nothing. The variable has been set but it is empty. If I print $this->curpath inside core.php it prints fine.

How can I access this variable?

  • 写回答

3条回答 默认 最新

  • dtgta48604 2012-06-20 11:41
    关注

    Try this

    include_once('core.php');
    $core = new Core('test', 'path');
    $core->loadController('property');
    
    class Property extends Core
    {
     function __construct($date)
    {
        print $date->curpath;
    }   
    }
    
    
    
    class Core
    {
    public $uri;
    public $curpath;
    
    function __construct($uri, $curpath)
    {       
        $this->uri = $uri;
        $this->curpath = $curpath;
    }
    
    
    // Load the controller based on the URL
    function loadController($name)
    {       
        //Instantiate the controller
        require_once($name.'.php');
        $controller = new $name($this);
    }
    
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 Matlab在app上输入带有矩阵形式的初始条件发生错误
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址
  • ¥15 elmos524.33 eeprom的读写问题
  • ¥15 用ADS设计一款的射频功率放大器