Forum Moderators: coopster

Message Too Old, No Replies

Object variable scope

         

jlvollmer

12:40 pm on Aug 21, 2009 (gmt 0)

10+ Year Member



I have a class named include_Ccs.php. In it I have another class (the popular Snoopy class) which is included using an include statement:

<?php
include ('snoopy.class.php');

class ccs
{
private $_name;
private $_skey;

public function __construct()
{
global $snoopy;
$snoopy = new Snoopy;
}

public function priorityMsg()
{
if($snoopy->fetchform($priorityUrl)) {
$formelements = $snoopy->results;
}

}
What I am needing is for the $snoopy object to be available in the other functions in the class. I tried declaring it as global but when I try to use a function (fetchform) of this Snoopy object in other functions in the Ccs class, I get an error as shown:

Fatal error: Call to undefined method stdClass::fetchform() in /home/public_html/include/include_Ccs.php on line 102

So what am I doing wrong? Is it a scope problem?

eelixduppy

12:36 am on Aug 26, 2009 (gmt 0)



For something like this, you are going to want to create a "snoopy" variable within the class. So for example, something like this:

class ccs
{
private $_name;
private $_skey;
private $snoopy;

public function __construct()
{
$this->snoopy = new Snoopy();
}
...etc...
}

Then within the other functions, you would use

$this->snoopy
instead of just
$snoopy
.

Try that and see what happens. :)

jlvollmer

12:03 pm on Aug 26, 2009 (gmt 0)

10+ Year Member



That works like a charm. Thanks a bunch.