SitePoint Sponsor |
|
User Tag List
Results 1 to 24 of 24
Thread: multiple extends
-
Dec 14, 2005, 04:32 #1
- Join Date
- Jan 2005
- Location
- blahblahblah
- Posts
- 1,447
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
multiple extends
Hello,
still coding under PHP 4.
I have a few apps. There are some recursive methods. As my stuff is coded now, these methods are coded in each application. I would like to group them in one class and make this class the parent class of all the parent classes of my various applications.
Like:
PHP Code:class MainParent {
//share all the methods common to all apps
}
class Application1 extends MainParent {
//this class is the parent class of all the Application1 classes and contains the methods used by all the Application1 classes.
}
class Application2 extends MainParent {
//this class is the parent class of all the Application2 classes and contains the methods used by all the Application2 classes.
}
Now to what refers parent:: in the application subclasses? or $this-> ?
Like in:
PHP Code:class DoSomething extends Application2 {
function DoSomething(){
parent::sharedMethod();
}
function DoSomethingElse(){
$var = $this->foo;
}
}
-
Dec 14, 2005, 04:49 #2
- Join Date
- Nov 2004
- Location
- Ankh-Morpork
- Posts
- 12,158
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
$this will refer to the instance itself, i.e., a DoSomething object.
parent refers to the parent class, i.e. Application2.Birnam wood is come to Dunsinane
-
Dec 14, 2005, 04:59 #3
- Join Date
- Jan 2005
- Location
- blahblahblah
- Posts
- 1,447
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Then how can reach the parent of the parent?
parent:: parent::sharedMethod();
?
-
Dec 14, 2005, 05:11 #4
- Join Date
- Nov 2004
- Location
- Southampton, UK
- Posts
- 537
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
$this will refer to the instance itselfPHP Code:<?
class basebase {
function speak()
{
print "hello";
}
}
class base extends basebase { }
class child extends base {
function child()
{
$this->speak();
}
}
$c = new child();
?>
-
Dec 14, 2005, 08:13 #5
-
Dec 14, 2005, 08:37 #6
- Join Date
- Nov 2004
- Location
- Ankh-Morpork
- Posts
- 12,158
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by jjshell
PHP Code:MainParent::sharedMethod();
I.e., unless you override sharedMethod() in Application2 and/or DoSomething, you can simply call it like this:
PHP Code:$this->sharedMethod();
Birnam wood is come to Dunsinane
-
Dec 14, 2005, 09:23 #7
- Join Date
- Jan 2005
- Location
- blahblahblah
- Posts
- 1,447
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Thanks for your replies.
I searched php.net for MainParent:: but couldn't find anything about it... Do you have some reference about it?
-
Dec 14, 2005, 09:27 #8
- Join Date
- Nov 2004
- Location
- Southampton, UK
- Posts
- 537
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
It's worth noting that you will have some fun with include() and require() functions calls...
@jjshell: I suspect that MainParent is a made up class
-
Dec 14, 2005, 09:35 #9
"MainParent" is the name of the parent parent's class.
If you don't want to statically call it by name, you can just pass it up the chain too.
PHP Code:<?php
class MainParent {
function sharedMethod() {
echo 'MainParent::sharedMethod()';
}
}
class Application2 extends MainParent {
function sharedMethod() {
parent::sharedMethod();
}
}
class DoSomething extends Application2 {
function DoSomething() {
parent::sharedMethod();
}
function DoSomethingElse() {
$var = $this->foo;
}
}
$doit = new DoSomething();
Code:MainParent::sharedMethod()
-
Dec 14, 2005, 11:17 #10
- Join Date
- Nov 2004
- Location
- Ankh-Morpork
- Posts
- 12,158
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by jjshell
Originally Posted by jjshell
Birnam wood is come to Dunsinane
-
Dec 14, 2005, 14:59 #11
- Join Date
- Sep 2003
- Location
- Glasgow
- Posts
- 1,690
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
"Application" classes are almost certainly trying to do too much. I think you'll need to split this up into smaller, more focussed classes with discrete roles.
Once you've encapsulated everything nicely you then need to figure out how to get everything talking to each other... Studying design patterns will help to give you some ideas. Inheritance is definitely not the way to do this and should be reserved for situations where the two classes are basically the same thing.
-
Dec 15, 2005, 01:47 #12
- Join Date
- Jan 2005
- Location
- blahblahblah
- Posts
- 1,447
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
What patterns could fit?
-
Dec 15, 2005, 10:04 #13
- Join Date
- Jan 2005
- Location
- blahblahblah
- Posts
- 1,447
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Is it the same with variables? Are they all shared? I seem to have a hard time getting them...
PHP Code:class MainParent {
var $var;
function MainParent()
{
$this->var = 'foo';
}
}
class Application extends MainParent {
}
class DoSomething extends Application {
function DoSomethingElse(){
return $this->var;
}
}
-
Dec 15, 2005, 12:13 #14
- Join Date
- Sep 2003
- Location
- Glasgow
- Posts
- 1,690
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
You must explicitly call a parent constructor, eg:
PHP Code:class DoSomething extends Application
{
function DoSomething(){
$this->MainParent();
}
function DoSomethingElse(){
return $this->var;
}
// etc
-
Dec 15, 2005, 12:48 #15
- Join Date
- Sep 2003
- Location
- Glasgow
- Posts
- 1,690
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by jjshell
If you're starting out with OOP you won't get it right first time - even experienced programmers won't get it right first time. It's a kind of evolutionary process. The forces of (un)natural selection are questions such as are the objects too tightly coupled? Are they cohesive? What happens if you stress the design with a certain type of change such as a switch to a different database? If you're testing, the bottom line is do the unit & etc tests run green?
It's a strange mix of philosophy and engineering. You're free to invent your own entities to carry out particular roles although they also have to relate closely to practical requirements. You'll get killed in programming with any lack of clarity all the way from variable and method names right up to the top level client requirements and the first step is to identify exactly what your requirements are then code to these. Modern practices like Extreme Programming put a strong emphasis on clear requirements and use testing to stay focussed on these.
http://c2.com/cgi/wiki?CouplingAndCohesion
http://www.developerspot.com/tutoria...ent/page1.html
-
Dec 15, 2005, 12:56 #16
- Join Date
- Sep 2004
- Location
- Zagreb, Croatia
- Posts
- 830
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by McGruff
PHP Code:class MainParent
{
var $var;
function MainParent()
{
$this->var = 'foo';
}
}
class Application extends MainParent
{
}
class DoSomething extends Application
{
function DoSomethingElse(){
return $this->var;
}
}
$test = new DoSomething();
echo $test->DoSomethingElse();
-
Dec 15, 2005, 13:06 #17
- Join Date
- Sep 2003
- Location
- Glasgow
- Posts
- 1,690
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Oops my mistake. I got mixed up with all those DoSomething's.
-
Dec 15, 2005, 13:08 #18
- Join Date
- Dec 2004
- Location
- Yorkshire, England
- Posts
- 676
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by McGruff
I took quite a while to warm to object-oriented PHP.
In fact, for a while, I was doin' it while not diggin' it!
In the end, the immediate benefits were much tidier code, which meant more manageable structure, which meant saved time .. which meant saved money.
Eventually, many more benefits come to fore...
-
Dec 16, 2005, 01:46 #19
- Join Date
- Jan 2005
- Location
- blahblahblah
- Posts
- 1,447
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
The mainparent class isn't instantiated... maybe that's why why I can't get it? It's like the Application class can use the vars set in MainParent but not DoSomething...
Does anyone have an idea why? Any usual suspect around?
Last edited by jjshell; Dec 16, 2005 at 02:39.
-
Dec 16, 2005, 04:13 #20
- Join Date
- Nov 2004
- Location
- Ankh-Morpork
- Posts
- 12,158
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
The example you posted works exactly as it is. I pasted your code into an editor and added two lines:
PHP Code:$foo = new DoSomething();
echo $foo->DoSomethingElse();
fooBirnam wood is come to Dunsinane
-
Dec 16, 2005, 08:11 #21
- Join Date
- Jan 2005
- Location
- blahblahblah
- Posts
- 1,447
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Ok fine... Got it.
Now please take a look at that scenario:
PHP Code://MainParent() is never instantiated
class MainParent {
var $var; //var should be available to all the children
var $bar; //bar should be available to all the children
function MainParent()
{
$this->bar = 'bar';
}
}
class Application extends MainParent {
var $doSomething;
function Application()
{
$this->var = 'foo';
$this->doSomething = new DoSomething();
}
function getVar()
{
return $this->doSomething->returnVar();
}
function getBar()
{
return $this->doSomething->returnBar();
}
}
class DoSomething extends MainParent {
function DoSomething()
{
parent::MainParent();
}
function returnVar()
{
return $this->var;
}
function returnBar()
{
return $this->bar;
}
}
$foo = new Application();
echo $foo->getVar();
echo $foo->getBar();
thank you all so much for your support and patience by the way
-
Dec 16, 2005, 09:49 #22
- Join Date
- Sep 2004
- Location
- Zagreb, Croatia
- Posts
- 830
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by jjshell
-
Dec 16, 2005, 15:05 #23
- Join Date
- Nov 2004
- Location
- Ankh-Morpork
- Posts
- 12,158
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
You need to call the base class constructor in Application's constructor, since you supply an explicit constructor (base class constructors are only called implicitly if there is no explicit constructor in the derived class).
PHP Code:function Application()
{
// call the base class constructor
$this->MainParent();
$this->var = 'foo';
$this->doSomething = new DoSomething();
}
Birnam wood is come to Dunsinane
-
Dec 17, 2005, 12:10 #24
- Join Date
- Jan 2005
- Location
- blahblahblah
- Posts
- 1,447
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Great Tommy!
thanks
Bookmarks