scope of php objects...some advice

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
definewebsites
Forum Newbie
Posts: 19
Joined: Thu May 01, 2008 9:51 pm

scope of php objects...some advice

Post by definewebsites »

Hello there,

I am building a web application using an MVC layout. I have all requests routed to my index page from which I parse the request and route to the appropriate controller/model/view.

I've realised that when I declare my objects in my index page such as:

Code: Select all

 
$db = new db();
 
By the time I get to my controller and try to use this variable/object $db, I get an error message saying something like "Trying to perform operation on non-object".

This has led me to declare my objects in each of my controllers as above which in many ways I feel is unnecessary repetition of code. I would appreciate any suggestions/advice as to why this could be happening..

Thanks
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: scope of php objects...some advice

Post by onion2k »

An object deals with scope in the same way as a function ... something defined outside of the object isn't available inside the object unless it's passed to it. Personally, I define things like database connections in an include file, then pass them to objects as an argument in the constructor... eg

Code: Select all

class myObject {
  function __construct(&$db) {
    $this->db = $db;
  }
}
 
$db = new Database_Connection();
$object = new myObject($db);
 
The & before $db in the constructor argument list means it's a reference rather than a copy. That's not especially important most of the time but for some things, like logging for example, it's helpful if everything goes through the same object.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: scope of php objects...some advice

Post by Christopher »

definewebsites wrote:I am building a web application using an MVC layout. I have all requests routed to my index page from which I parse the request and route to the appropriate controller/model/view.
My suspicion is that you have gotten ahead of yourself design-wise. MVC is not a "layout", though the Front/Action Controller architecture you are attempting may have a file/directory layout. Get the basics right in as simple a way as possible, and then apply these patterns as problems arise.
onion2k wrote:

Code: Select all

class myObject {
  function __construct($db) {
    $this->db = $db;
  }
}
 
$db = new Database_Connection();
$object = new myObject($db);
 
The & before $db in the constructor argument list means it's a reference rather than a copy. That's not especially important most of the time but for some things, like logging for example, it's helpful if everything goes through the same object.
Definitely pass objects into other objects to make them available in that scope. Also, the ampersand is not necessary in PHP5 because object vars are all handles -- which are like references.
(#10850)
definewebsites
Forum Newbie
Posts: 19
Joined: Thu May 01, 2008 9:51 pm

Re: scope of php objects...some advice

Post by definewebsites »

Thanks for the tips guys, I do appreciate. I will look into it again..
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: scope of php objects...some advice

Post by josh »

A class is not an object, think of a class as the blueprint of the object, you still have to build objects, which is called instantiating them
fredrik
Forum Newbie
Posts: 13
Joined: Sun Nov 04, 2007 4:41 am

Re: scope of php objects...some advice

Post by fredrik »

Someone that don't know how to instantiate objects talking about building an MVC application? wtf has the world come to.
definewebsites
Forum Newbie
Posts: 19
Joined: Thu May 01, 2008 9:51 pm

Re: scope of php objects...some advice

Post by definewebsites »

I actually do know how to instantiate an object. The folks who responded to my question did not quite understand what I was trying to explain, they assumed that I was talking about how to instantiate an object. I decided not comment on the misconception but to let it go and try to figure it out for myself.

It is clear that you also did not understand my question hence the comment you're making.

Cheers
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: scope of php objects...some advice

Post by josh »

I get an error message saying something like "Trying to perform operation on non-object".
If you get that error it is because you're using an OOP operator on a variable that does not contain a reference to an object, could mean you didn't instantiate it could mean you typ-oed the variable name, its a syntax error though
crazycoders
Forum Contributor
Posts: 260
Joined: Tue Oct 28, 2008 7:48 am
Location: Montreal, Qc, Canada

Re: scope of php objects...some advice

Post by crazycoders »

Not, logical error only, syntax errors make the compiler crash, this did not :)
Post Reply