Current location: Hot Scripts Forums » Programming Languages » PHP » objects of objects


objects of objects

Reply
  #1 (permalink)  
Old 01-01-04, 11:35 PM
Skeleton Man Skeleton Man is offline
Community Liaison
 
Join Date: Jun 2003
Location: Australia
Posts: 406
Thanks: 0
Thanked 0 Times in 0 Posts
objects of objects

Just wondering, how would I code a class that worked as follows:

PHP Code:

<?

$do 
= new DoStuff;

$foo $do->do_something()->do_something_else();
?>
OR:

PHP Code:

<?

$do 
= new DoStuff;

$foo $do->do_something();
$bar $foo->do_something_else();
?>
I am fammiliar with creating a basic class, but not having one object on top of another one. I'm not even sure if PHP supports this like perl does (this type of construct is very common in perl, but I have not seen it in php).
Reply With Quote
  #2 (permalink)  
Old 01-02-04, 02:46 AM
NeverMind's Avatar
NeverMind NeverMind is offline
Community VIP
 
Join Date: Aug 2003
Location: K.S.A
Posts: 2,257
Thanks: 0
Thanked 2 Times in 1 Post
you mean you have a function inside another function and you want to call the child function !? why did you make it like this ? it's just not needed ..
try calling them like this :
PHP Code:

$do = new DoStuff;


$foo $do->do_something();
$foo $do->do_something_else(); 
or you could call these functions without creating the object !! the opereator :: does this :
PHP Code:

CLASS car {

 function 
tyre () {
   echo
'what ever';
 }
 function 
door() {
   echo
'this door is bad !';
 }
}
//calling them with the class name without creating the object :
car::tyre();
car::door();

//or you could just create the object and call it :
$mycar = new car;

$mycar->trye();
$mycar->door(); 
the good thing about :: is that if your class is large you can use this operator to run the functions without processing the whole object ...

what ever was the level of the function you can just call it ..
__________________
PHPSimplicity
We don't need a reason to help people - Zidane [FF9]

Last edited by NeverMind; 01-02-04 at 02:53 AM.
Reply With Quote
  #3 (permalink)  
Old 01-02-04, 03:06 AM
Skeleton Man Skeleton Man is offline
Community Liaison
 
Join Date: Jun 2003
Location: Australia
Posts: 406
Thanks: 0
Thanked 0 Times in 0 Posts
Hi,

Thanks for that, but basic classes and objects like that aren't what I want.
I was wanting to write a basic socket class for reading writing to an array of sockets (up to a dozen open sockets at once, hence the need for my own functions). If this cannot be done in PHP, I'll try perl instead, as I know for certain this kind of construct is supported. Maybe this example clarifies things:

PHP Code:

<?


require ("./socket.php");

$hostname[0] = 'foobar.com';
$port[0] = 123;

$hostname[1] = 'foo.com';
$port[1] = 456;


$data "some data here";

$socket = new Socket;

$sockets[0] = $socket->connect($hostname[0],$port[0]);
$sockets[1] = $socket->connect($hostname[1],$port[1]);

$sockets[0]->send($data);
$sockets[1]->send($data);

$result[0] = $sockets[0]->read(1024); // Read up to 1024 bytes.
$result[1] = $sockets[1]->read(1024);

?>
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump


All times are GMT -5. The time now is 07:53 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.