简体   繁体   English

PHP对象继承的麻烦

[英]trouble with php object inheritance

I am building a page through a series of include files. 我正在通过一系列包含文件来构建页面。 Many (not all) of the include files are classes of various things that I need stored as objects. 许多(不是全部)包含文件是我需要存储为对象的各种事物的类。 For instance, one of my pages is: 例如,我的页面之一是:

 class site {
     var $siteid;
     var $sitename;

     function __construct($id, $name) {
        $this->siteid = $id;
        $this->sitename = $name;
     }

     function get_siteid(){
        return $this->sitename;
 }

and then on another page I have: 然后在另一页上,我有:

$site = new site("4","My Site");

So, on a subsequent include page I create another class called "page". 因此,在随后的包含页面上,我创建了另一个称为“页面”的类。 While creating this class I need to reference the siteid value instantiated previously for $site, but I can't seem to get at it. 在创建此类时,我需要引用先前为$ site实例化的siteid值,但似乎无法理解。

I've tried $site->get_siteid() but I get a message that says "undefined variable." 我已经尝试过$ site-> get_siteid(),但是收到一条消息,提示“未定义的变量”。

Strangely, on a regular HTML page later on, I am able to get the site id simply with $site->siteid, but from what I have read this is not a good practice, and this also doesn't work within the page class anyway. 奇怪的是,在稍后的常规HTML页面上,我仅能通过$ site-> siteid来获取站点ID,但是从我的阅读来看,这不是一个好习惯,并且在页面类中也不起作用无论如何。

I'm still pretty new to OO coding and so I am sure I am missing something pretty basic here, but have tried a lot of things and cannot seem to make it work. 我对OO编码还很陌生,因此我确定我在这里缺少一些基本的知识,但是尝试了很多事情,但似乎无法使其工作。

Thanks in advance. 提前致谢。 :) :)

Firstly, since you're using PHP5, use access specifiers when declaring properties and methods: 首先,由于您使用的是PHP5,因此在声明属性和方法时使用访问说明符:

Change: 更改:

var $siteid;
var $sitename;

To: 至:

public $siteid;
public $sitename;

Or make them private or protected if preferred. 或者将它们privateprotected如果需要)。 See the manual for more info on visibility. 有关可见性的更多信息,请参见手册

I've tried $site->get_site(id) but I get a message that says "undefined variable." 我试过$ site-> get_site(id),但收到一条消息,内容为“未定义的变量”。

There is no method called get_site . 没有名为get_site方法。 There is one called get_siteid but it inexplicably returns the site name. 有一个名为get_siteid的名称,但它莫名其妙地返回了站点名称。 You'll want to straighten that out. 您将要理顺。

I am able to get the site id simply with $site->siteid, but from what I have read this is not a good practice 我可以简单地通过$ site-> siteid来获取站点ID,但是从我阅读的内容来看,这不是一个好习惯

There's no point in making getters/setters that simply return/set member variables. 使获取器/设置器仅返回/设置成员变量没有意义。 Just declare the member public and access it directly. 只需将成员声明为公开并直接访问即可。 Nothing wrong with that. 没有错。

HTTP is a connectionless protocol. HTTP是无连接协议。 So state based information is not saved between requests. 因此,基于状态的信息不会在请求之间保存。 The object that is instantiated (eg. $site) will not be maintained between pages. 被实例化的对象(例如$ site)将不会在页面之间维护。

If you have persistent data that you need to store objects you can serialize the objects and store it in a mysql table or a file. 如果您需要存储对象的持久性数据,则可以序列化对象并将其存储在mysql表或文件中。 Then you can retrieve the serialized object by a using a key and the deserialize it and use it. 然后,您可以使用键检索序列化的对象,然后反序列化并使用它。

Several things 好几件事

  1. Intentionally or not, you have a typo. 是否有意输入错字。 Your method is named get_siteid() but you reference $site->get_site(id) 您的方法名为get_siteid()但是您引用了$site->get_site(id)
  2. id is not a valid variable, you should be using $id id不是有效的变量,您应该使用$id
  3. You're method doesn't receive a parameter but you're sending one 您的方法未收到参数,但您正在发送一个参数
  4. The reason $site->siteid works is because site::$siteid is public. $site->siteid起作用的原因是site::$siteid是公共的。 To prevent this, make the variable protected or private . 为防止这种情况,请将变量设置为protectedprivate

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM