Click to See Complete Forum and Search --> : [RESOLVED] Nesting Classes VIA include
Kyleva2204
09-08-2008, 03:22 PM
The title doesn't really do this question justice but what ever.
So say you have a class, and in once of the class's functions, you make another class..
Doesn't work because PHP doesn't allow nesting classes, I got that part.
BUT if you make another PHP file with a class in it, and then include it into that function... How does PHP handle this? I know it allows it, but how does it parse the class? Does it put it at the top of the parent document, or does it make the class inside the function?
The reason I'm asking is I want the new class to access the parent class..
Any ideas?
Thanks,
Kyle
Jeff Mott
09-08-2008, 03:32 PM
BUT if you make another PHP file with a class in it, and then include it into that function... How does PHP handle this?Check out example 2 in the include (http://www.php.net/include/) docs.
The reason I'm asking is I want the new class to access the parent classSounds like your new class needs to extend (http://www.php.net/manual/en/keyword.extends.php) the parent class.
NogDog
09-08-2008, 06:09 PM
Without know the particular functionality you are trying to achieve, it's hard to suggest the "correct" solution ("correct" because there's not always a single best solution).
You might extend the parent class as Jeff suggests. Conversely, you might pass one class to the other as part of its constructor (actually, pass an object instantiated from one class to the other class's constructor). Perhaps one class could consist only of static properties and methods, which could then be accessed directly via the other class via the "::" operator.
But again, without understanding the "why", it's hard to recommend a "how"; but usually if I need one class to access another, I do it by passing an object to the class constructor, which makes it an obvious and required part of that class's interface, especially if you use "type hinting" in the constructor's parameter list.
Kyleva2204
09-08-2008, 07:20 PM
Thank you both for the reply.
@NogDog
my mail goal was just to share one function from one class to another.. The function would be parent of the class I want to retrieve it from.
For example, this is how I would intuitively do it.. but doesn't work.
File1.php
class my_class{
function my_class{
include('file2.php');
$other_class = new my_OTHER_class;
}
function other_function(){
echo 'Stuff';
}
}
file2.php
class my_OTHER_class{
function my_OTHER_class(){
parent::other_function();
}
}
This is just an example of what I want to do.
Thanks again :)
NogDog
09-08-2008, 08:08 PM
If the "child" function is truly a child of the parent, i.e. it is a more specific "thing" of the same general type as the parent, then I would use inheritance:
Class TheParent
{
public function hello()
{
echo "hello world";
}
}
class TheChild extends TheParent
{
public function my_hello()
{
$this->hello();
echo ", and goodbye";
}
}
$test = new TheChild();
$test->my_hello();
If, instead, the "child" is really not a more specific instance of the "parent", but rather they are different things with one simply being used by the other, then I would pass the "used" object to the "user":
class Foo
{
function hello()
{
echo "hello world";
}
}
class Bar
{
public function __construct(Foo $foo)
{
$this->foo = $foo;
}
public function my_hello()
{
$this->foo->hello();
echo ", and goodbye";
}
}
$test = new Bar(new Foo());
$test->my_hello();