Re: Classes and Functions

From: Date: Wed, 02 Nov 2005 23:59:47 +0000
Subject: Re: Classes and Functions
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-225006@lists.php.net to get a copy of this message
On Wed, 2005-11-02 at 18:56 -0500, Unknown Unknown wrote: > Hi everybody i have a class that i reference with: > $DB= new DBInterface; > outside a function it works fine, but using $DB inside a function changes > the data type i think... i get an error saying i'm using a method on a > non-object... > any help appreciatted If you are attempting to access the variable $DB inside the function scope, like this: $DB = new DBInterface; function doSomething() { $DB->doSomethingElse(); } Then it will not work unless you either pass $DB to doSomething(), or declare $DB as a global at the start of the function. Like this: function doSomething() { global $DB; $DB->doSomethingElse(); } Or access $DB through the globals superglobal. RTFM. -- Jasper Bryant-Greene General Manager Album Limited e: jasper@album.co.nz w: http://www.album.co.nz/ b: http://jbg.name/ p: 0800 4 ALBUM (0800 425 286) or +64 21 232 3303 a: PO Box 579, Christchurch 8015, New Zealand

« previous php.general (#225006) next »