I've got an interesting question, and one that I cannot find the answer to in the PHP documentation.

Does PHP support nested classes? For example:

Class Bar {
var $barValue;

function Bar($init) {
$this->barValue = $init;
}

function printValue() {
echo $this->barValue;
}

}

Class Foo {
var $fooBar;

function Foo() {
$this->fooBar = new Bar("Hello");
}

function getBar() {
return $this->fooBar;
}
}

Ok, here's my "main" code:

<?

$tmp = new Foo();
$tmp->getBar()->printValue();

?>

When trying to call "printValue" on the nested class, I always receive a message telling me that I'm trying to access a class function from an object that is not an instance of the given class.

So I tried this:

<?
$tmp = new Foo();
$tmp2 = $tmp->getBar();

$tmp2->printValue();

?>

Same error. Does PHP not support nesting classes in this manner?

If it does, how do I access methods that are members of a nested class?

On a side note - can I create an ftp connection resource in a class, register that class as a session variable, and then continually refer to that ftp connection? Or must I open/close a connection for every operation? I suppose I have the same question regarding mysql connections... I've tried using mysql_pconnect, but with no success.

Thanks,
Will

    You code does not appear to have any nested classes !!!.

      Yes, I should have been more clear...

      the class definitions are not contained within one another; but the class "Foo" contains an object of the class "Bar"...

      Thanks,
      Will

        You can add the 'class $a extends $b' where $a is the main class and $b is the 'nested' class.

        That means any variable in class $a will exist is class $b.

          Write a Reply...