The Cache: Technology Expert's Forum
 
*
Welcome, Guest. Please login or register. September 17, 2019, 12:01:26 AM

Login with username, password and session length


Pages: [1]
  Print  
Author Topic: Adding methods to class  (Read 1869 times)
dink
Expert
****
Offline Offline

Posts: 349


View Profile
« on: January 04, 2008, 10:02:33 PM »

Hola dudettes.  Anno nuevo fleecydadamente!

I have this class (webRequest2) that doesn't have exactly what I need for a particular task.

I have a few functions that I would like to run when I get the results from a webRequest.
I can think of three ways to accomplish same.  1.  Write a new class and set it to extend
webRequest, or 2.  Write a new class and just require both classes at run time, or 3.  Put
the new functions in the original class and rename it webRequest2_ds.

The advantage I see with option 1 is that the new functions would inherit all of the goodies
available in webRequest.

Which of the above (or are there better alternatives) would fit the 'best practice' guidelines?
Are there any hidden gotchas for any of the choices? 

OOOOOP is way far down on my list of 'things I know', plus I'm making the transition from php1.0
to php5.2.2 (some fun too!), so any guidance would be greatly appreciated.

el Dinko
Logged

[quote Nutballs]
the universe has a giant fist, and its got enough whoop ass for everyone.
[/quote]
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #1 on: January 04, 2008, 11:25:35 PM »

It was built to extend. Option 1 is the most correct way, although option 2 is certainly a possibility, it is not to my taste.

Option 3 is a nono. The idea is to make it so that your functionality is not broken or erased if I update the class and you want my updates. See, if you mod the class then I do, you'll undo your mods by taking mine on. If your class extends mine, and provided I've done my work correctly, then you just copy the new code into your class file and the new code will be available to your inherited class without muss or fuss.

Another thought is that there are event pops in the class - onSuccess for example.

Consider this:
Code:
<?php
$req 
= new webRequest2();
$req->onSuccess 'handleSuccess';
$req->simpleGet('http://www.perkiset.org');

function 
handleSuccess($sender)
{
// Do some code because the request succeeded...
}
?>


In this example, I've added an event handler for the onSuccess event that is fired after a URL has been successfully pulled down. There is an onFailure as well.

(I noticed that there was a bug in all of this while I was writing this post, so I updated the code in the webRequest thread... - you'll want to go get it... please give'r a shot and let me know if it burps on you at all... looks good from my end)

This is another technique for when you have code you want executed that is really specific to <this> application and you'll never need to use it again... so there is no need to extend the class.

Ping back if you need more,
/p
« Last Edit: January 04, 2008, 11:30:43 PM by perkiset » Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
dink
Expert
****
Offline Offline

Posts: 349


View Profile
« Reply #2 on: January 05, 2008, 12:11:41 AM »

Cool.  Thanks Perk.  Option 1 it is.

The event pop sounds like some serious fun too.  That would fit with some of my other plans. 
Logged

[quote Nutballs]
the universe has a giant fist, and its got enough whoop ass for everyone.
[/quote]
Pages: [1]
  Print  
 
Jump to:  

Perkiset's Place Home   Best of The Cache   phpMyIDE: MySQL Stored Procedures, Functions & Triggers
Politics @ Perkiset's   Pinkhat's Perspective   
cache
mart
coder
programmers
ajax
php
javascript
Powered by MySQL Powered by PHP Powered by SMF 1.1.2 | SMF © 2006-2007, Simple Machines LLC
Seo4Smf v0.2 © Webmaster's Talks


Valid XHTML 1.0! Valid CSS!