/    Sign up×
Community /Pin to ProfileBookmark

Extending mult level classes

I am not being able to do this… is there any way ?

[code=php]
<?php
class one{
function one_(){
return “this is one”;
}
}

class two extends one{
function two_(){
return $this->one_();
}

class three extends two{
function three_(){
return $this_>two_();
}

}
// assume there’s class four which extends class three n like wise

$objThree = new three;

// here’s my problem
// error : Call to undefined method three::two_()
echo $objThree->three_();

}

?>
[/code]

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@ss1289Jul 30.2008 — I believe you have to call the extended class constructor within the subclass.

Like so:
[code=php]class two extends one{
//the constructor
function two() {
$this->one(); //constructor of class one.
}
function two_(){
return "this is two";
}
}
[/code]
Copy linkTweet thisAlerts:
@NogDogJul 30.2008 — Works OK when you fix some typos and misplaced braces:
[code=php]
<?php
class one{
function one_(){
return "this is one";
}
}

class two extends one{
function two_(){
return $this->one_();
}
} // added this missing "}"

class three extends two{
function three_(){
return $this->two_(); // changed "_>" to "->"
}
}
// assume there's class four which extends class three n like wise

$objThree = new three;

echo $objThree->three_();

// deleted misplaced "}"

?>
[/code]
Copy linkTweet thisAlerts:
@wastedauthorJul 30.2008 — Thankx for the correction NogDog ! Bt do u really think I have that simple class structure in my real app n copied n pasted in here ? There r hundreds of lines of code mate ! several functions in every classes. Fixing the braces n _> to -> isn't my problem to be solved.

N [B]ss1289 [/B]! i can't call into constructor like u suggest... coz i won't know how many functions gonna be added later... if i follow u i will have to call all everytime. And also i guess it's resource much consuming coz i will have to call them even if i don't have to use it. N i don't have to use all functions everywhere.

I just wanna know why i simply can't call a grand father class's method into grand child object simply. Why a grand child class method forgets his grand parent methods ?
Copy linkTweet thisAlerts:
@ss1289Jul 30.2008 — Read this http://us.php.net/manual/en/keyword.extends.php

Read the "Note" section and then read the 2nd Contributed Note made by Edward_nl

What I mentioned is something that's basically mandatory when using extends. You have to define the parent class within the child class or subclass.

You may have misread the code in my example. one() is different than one_(); one() is your constructor which is always a function with the same name as the class (You didn't have it in your example so I just quickly pretended like you did have one). one_() is just a member function.
Copy linkTweet thisAlerts:
@NogDogJul 31.2008 — Thankx for the correction NogDog ! Bt do u really think I have that simple class structure in my real app n copied n pasted in here ? There r hundreds of lines of code mate ! several functions in every classes. Fixing the braces n _> to -> isn't my problem to be solved.

N [B]ss1289 [/B]! i can't call into constructor like u suggest... coz i won't know how many functions gonna be added later... if i follow u i will have to call all everytime. And also i guess it's resource much consuming coz i will have to call them even if i don't have to use it. N i don't have to use all functions everywhere.

I just wanna know why i simply can't call a grand father class's method into grand child object simply. Why a grand child class method forgets his grand parent methods ?[/QUOTE]

Well, I ran the edited code in my previous reply and it worked just fine, and there is nothing wrong with the concept as you presented in your original post (other than those syntax errors). If the "real" code is not working, then perhaps we need to see the real code to find out where the real problem is.

The only thing I can think of without seeing the code (other than other syntax errors) is if you are declaring any of those functions as private, since private class attributes are not inherited. If that is the case, you need to define them as "protected" instead if you want them to be inherited but not public.
Copy linkTweet thisAlerts:
@ThinkMindsJul 31.2008 — Hi,

Have you looked into Object Cloning instead of multi-level inheritance? May be that will be easier to implement ....

Check this out:

http://ca.php.net/manual/en/language.oop5.cloning.php

Of course, I might be totally way off simply because I do not have a clue as to what your application is supposed to do ?

Cheers,

Mike
Copy linkTweet thisAlerts:
@wastedauthorJul 31.2008 — NogDog... thankx.. now that's the solution. ThinkMinds, thankx to you too... bt that was totally out of my need. Anyways.. thanks to you guys. It worked with [code=php]parent::method_name();[/code]
×

Success!

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