/    Sign up×
Community /Pin to ProfileBookmark

OOP problem w/ ::PARENT

Is there a way to couple ‘PARENT’ to the specific class, such as $this->PARENT::Function();

example:
Object B extends Object A
Object C accesses Object B through $GLOBAL

Object B’s PARENT::FUNCTION() now tries to access Object C’s parent – whereas I want it to access object B’s parent.

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@NogDogFeb 11.2008 — This works:
[code=php]
<?php
class A
{
public function example()
{
echo "This is class A.";
}
}

class B extends A
{
public function example()
{
parent::example();
}
}

class C
{
public function example()
{
$GLOBALS['b']->example();
}
}

$GLOBALS['b'] = new B();
$c = new C();
$c->example(); // outputs "This is class A."
?>
[/code]

However, it is rather contrary to the philosophy and goals of OOP, in that depending on a global value creates a strong coupling between objects, when loose coupling should be your goal.

Perhaps you should look into making class B a [url=http://www.php.net/manual/en/language.oop5.patterns.php]singleton pattern[/url], then get an instance of it within class C?
×

Success!

Help @ilbonparaurti 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,
)...