Re: classes confusion
- From: Henk Verhoeven <news1@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Thu, 28 Apr 2005 21:37:45 +0200
Ed Koren wrote:
I cannot seem to figure out how to access var $foo (defined in siteConfig class) in the userSession class.
Hi Ed,
Try to mae a difference between a class and an object (an instance of a class). The member variable $foo is only present in the instances, not in the class itself. So to create the variable you need to create an instance. And to access the instance later you need to put the instance in a variable:
$myConfig = new siteConfig();
Once you have the instance in a variable you can access the value of the member variable of the instance and print it:
print $myConfig->foo;
To access the $foo member varible from methods defined in the class, you can use the pseudo variable $self
class siteConfig {
var $foo = "bar";
function printBar() {
print $this->bar;
}to call the method: $myConfig = new siteConfig(); $myConfig->printBar();
Both the method and the member variable will be inherited by subclasses, so this works too:
$myConnect = new dbConnect();
$myConnect->printBar();
You can override the inherited method on the subclass:
class dbConnect extends siteConfig { function dbConnect() { $settings = parent::getSettings();
// connect to database
} function printBar() {
print "the value of bar is: ";
parent::printBar();
}You can have several instances in different variables with different values in their member variables:
$firstConfig = new siteConfig();
$firstConfig->foo = "candy";
$secondConfig = new siteConfig();
print $firstConfig->foo;
print $secondConfig->foo;
you can also have one instance reference one another: $firstConfig = new siteConfig(); $secondConfig = new siteConfig(); $firstConfig->nestedConfig =& $secondConfig; $secondConfig->foo = "nested"; print $firstConfig->nestedConfig->bar;
Now this is all setting a bad example, you should use getter and setter methods to create encapsulation, but that is lesson two.
I would like to access some config variables outside the class too, through an instance of the userSession class.
So why didn't you?
I read that a class can only have one parent in PHP, does this mean you cannot stack more than two classes on top of each other?You can have inheritance as deep as you want. But to what use? Is there any other class you need to subclass from siteConfiguration? If not, why not put all of its content into dbConnection? The same goes for dbConnection itself: why not put everything into userSession?
Maybe you want to express the theoretical notion that these are different things? That's ok, but then why subclass them from another? To make them different things you need to make different objects. If these objects are related you may give one a reference to another, like in the example above. With the inheritance you are saying 'a dbConnection is a kind of siteConfiguration' and 'a userSession is a kind of dbCennection'. Is that really what you want to express?
Greetings, success,
Henk Verhoeven, www.phpPeanuts.org
(i advice not to try phpPeanuts until you are comfortable with the basic principles of object orientation)
.
- Follow-Ups:
- Re: classes confusion
- From: Ed Koren
- Re: classes confusion
- References:
- classes confusion
- From: Ed Koren
- classes confusion
- Prev by Date: classes confusion
- Next by Date: Re: MS Access and PHP
- Previous by thread: classes confusion
- Next by thread: Re: classes confusion
- Index(es):