Welcome to WebmasterWorld Guest from 54.167.40.25

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Quick PHP OO question

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

Junior Member

5+ Year Member

joined:July 28, 2006
posts:168
votes: 0


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 July 4, 2008 (gmt 0)

Senior Member

WebmasterWorld Senior Member 5+ Year Member

joined:July 12, 2007
posts:766
votes: 0


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 July 5, 2008 (gmt 0)

Junior Member

5+ Year Member

joined:Dec 19, 2006
posts:92
votes: 0


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 July 7, 2008 (gmt 0)

Senior Member

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

joined:Nov 12, 2005
posts:5966
votes: 0


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