tags:

views:

37

answers:

3

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.

I've tried $site->get_siteid() but I get a message that says "undefined variable."

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.

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.

Thanks in advance. :)

+1  A: 

Firstly, since you're using PHP5, use access specifiers when declaring properties and methods:

Change:

var $siteid;
var $sitename;

To:

public $siteid;
public $sitename;

Or make them private or protected if preferred. See the manual for more info on visibility.

I've tried $site->get_site(id) but I get a message that says "undefined variable."

There is no method called get_site. There is one called get_siteid but it inexplicably returns the site name. 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

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.

webbiedave
I agree with everything except "There's no point in making getters/setters that simply return/set member variables." When you make getter/setters, you're future proofing but allowing abstraction to occur, providing a base for polymorphism, and can definitely make certain aspects of unit testing easier.
Peter Bailey
I agree, Peter. I should have written "there's no point in **worrying about** making getters/setters". I wanted to let Gary know that it's not bad to directly access member variables. I've seen many a class that uses getters/setters for no purpose other than they heard it's better to do it that way and some editors even auto-create these for you! Just wanted to make the point that it's ok to use public members where warranted. No stigma need be attached.
webbiedave
All - I have fixed the typo in my post... the function call should be (and was in my code) get_siteid() .
Gary
I am able to access the $site->siteid variable from within one of the included HTML pages... but when I am setting up the page class I am unable to access that variable in the __construct method... I just get a message that the $site variable is undefined. Should I be calling it a different way? (And I have changed the "var" to "public" - thanks. :)
Gary
@Gary: Perhaps this is a scope issue. Place `global $site;` as the first line in `Page::__construct()` to see if this allows access to the variable. Even better would be to create a member variable `Page::site` and pass `$site` to Page's constructor (`$page = new Page($site)`) then in __construct: `$this->site = $site`;`
webbiedave
@Dave - both of these suggestions did the trick, thanks! I should have thought about passing in the siteid to the page constructor.
Gary
Glad I could help.
webbiedave
+1  A: 

HTTP is a connectionless protocol. So state based information is not saved between requests. The object that is instantiated (eg. $site) will not be maintained between pages.

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. Then you can retrieve the serialized object by a using a key and the deserialize it and use it.

juxstapose
A: 

Several things

  1. Intentionally or not, you have a typo. Your method is named get_siteid() but you reference $site->get_site(id)
  2. id is not a valid variable, you should be using $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. To prevent this, make the variable protected or private.
Peter Bailey
The typo was unintentional. The method call in my code was get_siteid() and I've corrected the post. Thanks.
Gary