homepage Welcome to WebmasterWorld Guest from 23.23.57.144
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Quick PHP OO question
ryan_b83

5+ Year Member



 
Msg#: 3690578 posted 4:18 pm on Jul 4, 2008 (gmt 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();

 

PHP_Chimp

WebmasterWorld Senior Member 5+ Year Member



 
Msg#: 3690578 posted 5:08 pm on Jul 4, 2008 (gmt 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]

darrenG

5+ Year Member



 
Msg#: 3690578 posted 10:33 am on Jul 5, 2008 (gmt 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...

eelixduppy

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



 
Msg#: 3690578 posted 3:34 am on Jul 7, 2008 (gmt 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

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved