Class only available through if parent Class is called

Hey,

Not sure if I named the title correctly. I’m trying to learn how to write up a class that is available only through another class. I can’t find the correct words but I’ll give an example.

PDO Class and PDOStatement class

So when you do


$obj = new PDO('example');
$statement = $obj->prepare('$query');
//Now when you do $statement-> you get more methods
$statement->execute();

If you’re familiar with the PDO class you’ll notice you get even more methods once you use the statement class.

What I’m looking for is the proper name of this, so I can familiarize myself with it and find some examples online. Sorry for not making much sense!

thank you :eye:

Object composition?

The PDO object’s method is just returning an object of another type to you. Not much different than a function returning a string versus an array. You can do different things with an array than with a string.

The method PDO::Prepare returns a [url=http://www.php.net/manual/en/class.pdostatement.php]PDOStatement object.

ah yes perfect. this is exactly what I was looking for.

Ahh yes, that’s the same technique that PEAR’s MDB2 uses.


$res = $mdb2->query('SELECT * FROM users');
while ($row = $res->fetchRow()) {
    echo $row['id'] . "\
";
}

You could use something like the following to achieve the returned class object:


return new $resultSet($results);

Yes I found a tutorial online on how to create it.

I’m very surprised on how easy it is to actually do it. It also helps build great structure. Very nice!