Welcome to WebmasterWorld Guest from 54.163.100.58

Forum Moderators: coopster & jatar k

Quick PHP OO question

   
4:18 pm on Jul 4, 2008 (gmt 0)

5+ Year Member



Hello, I was wondering if I instantiate a class which has class varibles in it how could I access just the variable in an outside method rather than re-instantiating the class within the method. Here is an example


class siteBase{
function siteBase{
$this-var = "123";
}
}

class siteUtilities{
function someMethod{
//How would I access $siteBase->var without having to instantiate it?
}
}

$siteBase = new siteBase();
$siteUtilities = new siteUtilities();

$siteUtilities->someMethod();

5:08 pm on Jul 4, 2008 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



class siteUtilities extends siteBase{
function someMethod{
//How would I access $siteBase->var without having to instantiate it?
}
}

Then you can overload whatever you want. Have a look at [php.net...] and [php.net...] for more specific info on class constants.

[edited by: PHP_Chimp at 5:10 pm (utc) on July 4, 2008]

10:33 am on Jul 5, 2008 (gmt 0)

5+ Year Member



You can't access a variable declared inside a function. The function would either have to return the value stored in the variable, or the variable should be made a class attribute, in which case it can be accessed statically, or via inheritance...
3:34 am on Jul 7, 2008 (gmt 0)

WebmasterWorld Senior Member eelixduppy is a WebmasterWorld Top Contributor of All Time 5+ Year Member



You should be able to make the variable constant, too, in your class, then use the scope resolution operator to access it:

class siteBase {
const var = 123;
...
}
#
echo siteBase::var; #uses scope resolution operator
 

Featured Threads

Hot Threads This Week

Hot Threads This Month