Topic: Multilevel classes

In a few programs i've seen simliar to these (in the jpgraph gd lib among others):

$var->anothervar->more->setHeadline('blah');

What are they and how do i use them and for what?

The only thing i can relate to with this is the dotted format that java use:
system.out.println("test")
for instance...

any clarifications?

Re: Multilevel classes

It means that the object var has a member object called anothervar that has a member object called more that has a function called setHeadline(). Here's an example of a class that has a member object:

class One
{
    function scream()
    {
        print 'Whee!';
    }
}

class Two
{
    var blergh = new One();

    function asdf()
    {
        ....
    }
}

We can then create an object of the class Two:

$some_object = new Two();

and then access the scream function like this:

$some_object->blergh->scream();

"Programming is like sex: one mistake and you have to support it for the rest of your life."

Re: Multilevel classes

I'm not entierly sure i understand why this would be better or different then using the oldfashion:
$one = new One();
$two = new Two();

and working with those...

Re: Multilevel classes

Well, primarily because of the chaos we would be in if we didn't. If you look at my example above, one says that Two "has a" One. An object of class One is one of the parts of Two. When we create an object of class Two, it will automatically have an object of class One. Here's a more fitting example:

Lets say we are going to model a car using object oriented programming. The approach we take is to try to come up with all entities that would be suitable to make into classes. Example of such entities are: the car itself, wheels, the engine, pistons in the engine etc etc. We could first create a class Car that would represent the car as a whole. That class would then have four objects of class Wheel and one object of class Engine. The Engine class would in turn have six objects of type Piston. These are all examples of "has a" relationships (i.e. the car "has" an engine, the engine "has" six pistons etc.). The relationship is also called aggregation. If we model the car like that, we get everything nice and organized.

The above is all good, but what if we want to represent both regular cars and, say, trucks? Would we then have to create a new class from scratch to represent trucks? Yes and no. We would create a separate class for trucks, but we wouldn't have to write all the code once more. We would first create a class Vehicle. In Vehicle we define everything that a car and a truck have in common and we then let Car and Truck inherit from Vehicle. That way, we only have to add the code that differs. We say that Vehicle is the base class for Car and Truck. This would give us an "is a" relationship (i.e. a Car "is a" Vehicle, a trucks "is a" Vehicle).´

It it clearer now?

"Programming is like sex: one mistake and you have to support it for the rest of your life."

Re: Multilevel classes

Yes, it's a little clearer, it's actually almost a text-book example from a java-tutorial about class-inheritance, which i still have some headace trying to iron out =/

I get the general picture of how to use them, now i just need to figure out what parts goes together etc...