p2p.wrox.com Forums

Need to download code?

View our list of code downloads.

  Return to Index  

beginning_php thread: do objects have scope limitations?

Message #1 by spam@k... on Sat, 21 Sep 2002 16:10:03 -0500
I read over the page on scope on php.net:


Very informative but it didn't explicitly tell me the question I was wondering. Do objects have scope? If I have a page full of
functions, and at the top of that page I declare a database object (based on an included_once() db class) can I then use that object
inside the functions without limit? The object, I assume, will carry is properties inside of other functions? 

The php.net page had this, but I wasn't able to understand it:

function test_global_ref() {
    global $obj;
    $obj = &new stdclass;

function test_global_noref() {
    global $obj;
    $obj = new stdclass;

Executing this example will result in the following output: 

object(stdClass)(0) {


Although the keyword global is being used here, it doesn't seem to me that anything is being imported from outside the function,
because the object doesn't even get created until the next line. And even if there was a pre-existing object of that name, wouldn't
it get overwritten by the line that creates the new object? I don't understand what's going on here. And why would an object be
created with as if it was a reference to something else? And why it would it then be null? 

The code above seems to imply that if you create an object inside of a function you have to use the word global (rather than
return?) to all it to be reachable outside the function. 

The php.net page on classes added nothing to the above. 

If these questions are stupid I apoligize, but I'm new to objects. 

Message #2 by spam@k... on Sun, 22 Sep 2002 19:13:35
Okay, to answer my question, objects are just big, complicated variables, 
basically, so they have all the scope limitations as any other variable. I 
worked that out like this:

class formatDate
    var $todaysDate;  // Items in our shopping cart
     function formatDate () {
        $this->todaysDate = date("Y-m-d");
	return $this->todaysDate;

echo "First print out: <br>";
$date = new formatDate;

echo "<br><Br>Second print out:<br>";
echo $date->todaysDate; 

echo "<br><br>Third print out:<br>";
$today = $date->todaysDate;
echo $today;

function printDates() {
	echo "<br><br>This is the print out from inside the first 
function, the object is not imported with the 'global' keyword:<br>";
	echo $date->todaysDate;


function printDates2() {
	global $date;
	echo "<br><br>This is the print out from inside the function where 
the object has been made global:<br>";
	echo $date->todaysDate;



And the above output this:

First print out: 
object(formatdate)(1) { ["todaysDate"]=> string(10) "2002-09-22" } 

Second print out:

Third print out:

This is the print out from inside the first function, the object is not 
imported with the 'global' keyword:

This is the print out from inside the function where the object has been 
made global:

This might be a lot of work to figure out the obvious, but this I way I 
learn things, and I share it here for anyone else who is just learning 

Message #3 by "Dan Ostrowski" <dan@t...> on Sun, 22 Sep 2002 22:59:30
Probably why you are confused is because the first snippet you posted had 
to do with REFERENCING. Notice the "&" in that first snippet?

I think what was happening, was they were showing you the difference 
between global objects being used in functions and global objects being 
affected by references.

Objects can be used just like variables inside functions (or methods, or 
whatever you are calling them at the time) when you declare them as 
Global, but it's a bit more complicated than that, because Objects really 
are their own namespace.


  Return to Index