访问父类对象&通过 $this 变量?

发布于 2024-12-25 16:21:12 字数 1674 浏览 4 评论 0原文

我有一个主类“main”,我用它来存储配置和一些对象。但是,我无法访问对象和对象。通过 $this 扩展 main 的类中的变量。

这是我的一些主类:

<?php
class main{
    // the configuration vars
    public $config = array();
    // the current user variables
    public $uid; // contains the user ID
    public $gid; // contains the group ID
    // database variables
    public $DB; // contains the connection
    public $query_id; // contains the query ID
    public $query_count; // how many queries have there been?
    // cache variables
    public $cache;
    // start up functions
    public function startup(){
        // first, set up the caching
        if(function_exists('memcache_connect') AND $this->config['use_memcache'] == true){
            // start up memcache
            require_once('memcache.class.php');
            $this->cache = Cache::getInstance();
        }
        // now, set up the DB
        $this->connectToDatabase();
        // now, set up the user session
        $this->setUserSession();
        // are we logged in? If so, update our location
        if($this->uid > 0){
            // update location
            $this->updateUserSession();
        }
    }

这是另一个类的示例

<?php
class user extends main{
        public function viewProfile($uid){
                exit($this->config['session_prefix']); // displays nothing
                if($this->cache->exists('key')){ // fails

$config 值设置正确,我可以在 main 中读取它,但不能在任何子类中读取它。 $cache->exists 对象只会导致调用非对象上的成员函数exists()

在index.php 上,加载并设置了类。首先设置Main类,然后调用启动函数。配置数组在调用启动之前传递。

谁能建议我如何解决这个问题?

I have one main class "main", which I use to store the configuration and a few objects. However, I am unable to access the objects & variables in a class which extends main, through $this.

Here is some of my main class:

<?php
class main{
    // the configuration vars
    public $config = array();
    // the current user variables
    public $uid; // contains the user ID
    public $gid; // contains the group ID
    // database variables
    public $DB; // contains the connection
    public $query_id; // contains the query ID
    public $query_count; // how many queries have there been?
    // cache variables
    public $cache;
    // start up functions
    public function startup(){
        // first, set up the caching
        if(function_exists('memcache_connect') AND $this->config['use_memcache'] == true){
            // start up memcache
            require_once('memcache.class.php');
            $this->cache = Cache::getInstance();
        }
        // now, set up the DB
        $this->connectToDatabase();
        // now, set up the user session
        $this->setUserSession();
        // are we logged in? If so, update our location
        if($this->uid > 0){
            // update location
            $this->updateUserSession();
        }
    }

Here is an example of another class

<?php
class user extends main{
        public function viewProfile($uid){
                exit($this->config['session_prefix']); // displays nothing
                if($this->cache->exists('key')){ // fails

The $config value is correctly set up, and I can read it within main but not any sub class.
The $cache->exists object just results in Call to a member function exists() on a non-object.

On index.php, the classes are loaded and set up. The Main class is setup first, and the startup function is called. The configuration array is passed before startup is called.

Can anyone suggest how I can fix this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

尴尬癌患者 2025-01-01 16:21:12

您确定您的阵列设置正确吗?

这个简单的测试对我有用:

<?
class main {
    public $test = "Woot";
}

class test extends main {
    public function __construct() {
        echo $this->test;
    }
}

$t = new test();

仅出于测试目的,将变量 $test 添加到类 main 中(如我的示例中所示),并尝试在用户构造函数中回显它。

我会检查您的缓存是否确实设置正确。
错误可能出在这个 if 语句中:

if(function_exists('memcache_connect') AND $this->config['use_memcache'] == true){

不满足这两个条件之一,因此您的 $cache 变量将不会被初始化。

Are you sure your array is correctly set up.

This simple test works for me:

<?
class main {
    public $test = "Woot";
}

class test extends main {
    public function __construct() {
        echo $this->test;
    }
}

$t = new test();

Just for testing purposes, add a variable $test like in my example to the class main and try to echo it in the user constructor.

I would check if your cache is really set up correctly.
Probably the error is in this if statement:

if(function_exists('memcache_connect') AND $this->config['use_memcache'] == true){

One of these two conditions isn't met, so your $cache variable won't get initialized.

千里故人稀 2025-01-01 16:21:12

您可以使用 parent:: 引用父类中的变量。

parent::cache->exists('key')

you can use parent:: to referance variables in the parent class.

parent::cache->exists('key')
网白 2025-01-01 16:21:12

您可以通过 $this->attribute 访问父类的属性,所以问题应该是您没有调用父类的构造函数...

就像这段代码完美工作:

    <?php
    class foo {
        public $x="aa";

    }
    class bar extends foo {
        public function barbar()
        {
              echo $this->x;
        }
    }
    $b = new bar;
    $b->barbar(); 
    ?>

虽然您也可以通过 Parent 访问父类属性: :attr()

You can access parent class's attributes by $this->attribute, so the problem should be that you are not calling the constructor of the parent...

Like this code is perfectly working:

    <?php
    class foo {
        public $x="aa";

    }
    class bar extends foo {
        public function barbar()
        {
              echo $this->x;
        }
    }
    $b = new bar;
    $b->barbar(); 
    ?>

Though you can also access parent class properties by Parent::attr()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文