访问父类对象&通过 $this 变量?
我有一个主类“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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您确定您的阵列设置正确吗?
这个简单的测试对我有用:
仅出于测试目的,将变量 $test 添加到类 main 中(如我的示例中所示),并尝试在用户构造函数中回显它。
我会检查您的缓存是否确实设置正确。
错误可能出在这个 if 语句中:
不满足这两个条件之一,因此您的 $cache 变量将不会被初始化。
Are you sure your array is correctly set up.
This simple test works for me:
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:
One of these two conditions isn't met, so your $cache variable won't get initialized.
您可以使用
parent::
引用父类中的变量。you can use
parent::
to referance variables in the parent class.您可以通过 $this->attribute 访问父类的属性,所以问题应该是您没有调用父类的构造函数...
就像这段代码完美工作:
虽然您也可以通过 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:
Though you can also access parent class properties by Parent::attr()