PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
dull1554
Forum Regular
Posts: 680 Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W
Post
by dull1554 » Tue Jun 08, 2004 10:17 pm
if you have a class within a class
i.e.
when i initiate test (new test) does it also initiate test2 or would i have to do a "new test->test2"?
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Tue Jun 08, 2004 10:36 pm
dunno offhand.. it wouldn't in C last I used nested classes.. can always try it:
Code: Select all
class test
{
function test()
{
echo "in test()\n";
}
class test2
{
function test2()
{
echo "in test2()\n";
}
}
}
Weirdan
Moderator
Posts: 5978 Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine
Post
by Weirdan » Tue Jun 08, 2004 10:46 pm
Are you referring to PHP5?
PHP4 doesn't allow nested classes...
scorphus
Forum Regular
Posts: 589 Joined: Fri May 09, 2003 11:53 pm
Location: Belo Horizonte, Brazil
Contact:
Post
by scorphus » Tue Jun 08, 2004 10:47 pm
I think you can't do it in PHP 4. I'm not playing with PHP 5 so...
Scorphus
PrObLeM
Forum Contributor
Posts: 418 Joined: Sun Mar 07, 2004 2:30 pm
Location: Mesa, AZ
Contact:
Post
by PrObLeM » Tue Jun 08, 2004 11:25 pm
dull1554
Forum Regular
Posts: 680 Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W
Post
by dull1554 » Wed Jun 09, 2004 10:01 am
would that work in php 4?
or if im in php5 do i still need to extend it to the outter class?
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Wed Jun 09, 2004 12:41 pm
extends works in 4 and 5
dull1554
Forum Regular
Posts: 680 Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W
Post
by dull1554 » Wed Jun 09, 2004 2:31 pm
so using extends, it is possiable to nets classes, but if in 5 you car able to do so without extending the class?
do i have it right or is this way over my head?!?!?
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Wed Jun 09, 2004 2:42 pm
nested classes, as your original post has them, isn't extending the parent. The nested class is a component of its parent.. like how a function is apart of its parent class.
dull1554
Forum Regular
Posts: 680 Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W
Post
by dull1554 » Wed Jun 09, 2004 9:40 pm
so it would be more like this.....
Code: Select all
class test{
}
class test2 extends test{
function test3(){echo "test4";}
}
//then
$test = new test;
$test->test2->test3();
after typing this i just tried it and i get an error, what would i have to do to get this to work, i guess im totally missing the point.....
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Wed Jun 09, 2004 10:03 pm
Code: Select all
class test
{
function somefunc()
{
echo "joy!";
}
}
class test2 extends test
{
function somefunc()
{
echo "juice!\n";
test::somefunc();
}
}
$blah = new test;
$blah2 = new test2;
or something.. (not tested)
McGruff
DevNet Master
Posts: 2893 Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland
Post
by McGruff » Wed Jun 09, 2004 10:05 pm
Use inheritance if the two classes are genuinely of the same "type". Suppose you had a Rank class used for adding a ['rank'] key to search results. You might extend this with derived classes which define different types of ranking algorithmns.
It's usually better not to use inheritance if you have another option, ie aggregation or composition.
Code: Select all
<?php
/*
Aggregation:
*/
class Test
{
/*
param (object)
*/
function Test(&$test2)
{
$this->test2 =& $test2;
}
/*
etc..
*/
}
/*
Composition:
*/
class Test
{
function Test()
{
$this->test2 =& new Test2;
}
/*
etc..
*/
}
?>
Incidentally, that last one is known as the Factory pattern - see the design section at
http://www.phppatterns.com .
Last edited by
McGruff on Mon Aug 08, 2005 7:42 pm, edited 1 time in total.
dull1554
Forum Regular
Posts: 680 Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W
Post
by dull1554 » Thu Jun 10, 2004 6:00 am
allright, cool, i think i got it now and thanks for all the help