/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Nesting Classes VIA include

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

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@Jeff_MottSep 08.2008 — BUT if you make another PHP file with a class in it, and then include it into that function... How does PHP handle this?[/quote]Check out example 2 in the [url=http://www.php.net/include/]include[/url] docs.

The reason I'm asking is I want the new class to access the parent class[/quote]Sounds like your new class needs to [url=http://www.php.net/manual/en/keyword.extends.php]extend[/url] the parent class.
Copy linkTweet thisAlerts:
@NogDogSep 08.2008 — 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.
Copy linkTweet thisAlerts:
@Kyleva2204authorSep 09.2008 — 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
[code=php]
class my_class{
function my_class{
include('file2.php');
$other_class = new my_OTHER_class;
}

function other_function(){
echo 'Stuff';
}

}
[/code]


file2.php
[code=php]
class my_OTHER_class{
function my_OTHER_class(){
parent::other_function();
}
}
[/code]



This is just an example of what I want to do.

Thanks again ?
Copy linkTweet thisAlerts:
@NogDogSep 09.2008 — 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:
[code=php]
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();
[/code]

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":
[code=php]
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();
[/code]
×

Success!

Help @Kyleva2204 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...