Coder Profile - Show off your skills, get a coder profile.
 
 
 
  
Posted: 3.47 Years Ago 

VBAssassin
United Kingdom
Contrib Level: 17
Total Posts: 5,730
Hi ya guys,

Here is the original code
CODE: Copy / Restore  ::  Remove Scroll Bars
  1. <?php
  2.     class messages {
  3.         function add_message($message, $type = 'error') {
  4.  
  5.         }
  6.     }
  7.     //normal use
  8.     $message = new messages();
  9.     $message->add_message("Well done!", "confirm");
  10.     $message->add_message("Warning though, don't do this again");
  11. ?>
Now, what i need to do is add a new function...
CODE: Copy / Restore  ::  Remove Scroll Bars
  1. <?php
  2.     //saves a new user and returns a message
  3.     function add_new_user() {
  4.         return "the user does not exist";
  5.         return "the user has been added successfully";
  6.     }
  7. ?>
The problem is, the first message would have to be an error message, which is fine... however, the second needs to be a confirmation message... how do i pass that data to the add_message method of the messages class?

The solution... pass an array (optional), like this...
CODE: Copy / Restore  ::  Remove Scroll Bars
  1. <?php
  2.     //saves a new user and returns a message
  3.     function add_new_user() {
  4.         return array("the user does not exist");
  5.         return array("the user has been added successfully", "confirmation");
  6.     }
  7. ?>
So that you can then do this with the class
CODE: Copy / Restore  ::  Remove Scroll Bars
  1. <?php
  2.     $message = new messages();
  3.     $message->add_message(add_new_user());
  4. ?>
and it will automatically extract the second element from the array it recieves and use that as the type... otherwise, if it gets a string, it uses the second parameter in the method.

I've never used this technique before... but it saved me! Just wondering if there is a name or anything for it... like if a function calls itself it's called recursion... you got factory patterns, decorator patterns, etc... is there a name?

Also, has anyone else used this technique before?

Kind regards,
Scott
Posted: 3.47 Years Ago 

Izzmo
United States
Contrib Level: 12
Total Posts: 1,982
That's what I've always done, use an array. I wouldn't use text for the type though, I always use integers there and just make a key somewhere in my code to remember what each int means. I don't think I have to give you an example though!

C# offers better functionality in this dept, but as far as I know, this is really the only way of doing so in PHP.
Posted: 3.47 Years Ago 

VBAssassin
United Kingdom
Contrib Level: 17
Total Posts: 5,730
Post Quote - Direct Reference
The key is that it supports all of these...
CODE: Copy / Restore  ::  Remove Scroll Bars
  1. $message->add_message("Warning though, don't do this again");
  2. $message->add_message(array("Warning though, don't do this again"));
  3. $message->add_message("Warning though, don't do this again", "error");
  4. $message->add_message(array("Warning though, don't do this again", "error"));
Which all produce exactly the same result.

Also, i know using text for type is not really very good... using global definitions i don't like either though... having shit loads of those is just confusing. What would be really good is if i could enum it like:
CODE: Copy / Restore  ::  Remove Scroll Bars
  1. function add_message($message, $type = enum('normal', 'error', 'confirm')) {
  2.        //code here
  3. }
But PHP don't have enum datatypes

Kind regards,
Scott
Posted: 3.47 Years Ago 

xLink
United Kingdom
Contrib Level: 5
Total Posts: 164
why not just add a default value to the $type, and then just put a switch on it inside the function
CODE: Copy / Restore  ::  Remove Scroll Bars
  1. function add_message($message, $type = 'normal') {
  2.        switch($type){
  3.               //cases here
  4.        }
  5.        //rest of code
  6. }
Posted: 3.47 Years Ago 

VBAssassin
United Kingdom
Contrib Level: 17
Total Posts: 5,730
Post Quote - Direct Reference
It does, but that don't solve the problem. If a function returns a message and needs to change the default type then you can't. Thats the exact problem my solution solved.

Anyway, here's a new version of that class for izzmo...
CODE: Copy / Restore  ::  Remove Scroll Bars
  1. <?php
  2.  
  3.     class messages {
  4.  
  5.         const TYPE_ERROR = 1;
  6.         const TYPE_CONFIRM = 2;
  7.  
  8.         function add_message($message, $type = TYPE_ERROR) {
  9.  
  10.         }
  11.     }
  12.  
  13.     //normal use
  14.     $message = new messages();
  15.     $message->add_message("Well done!", messages::TYPE_CONFIRM);
  16.     $message->add_message("Warning though, don't do this again", messages::TYPE_ERROR);
  17.  
  18. ?>
Kind regards,
Scott
Posted: 3.47 Years Ago 

xLink
United Kingdom
Contrib Level: 5
Total Posts: 164
ahh i see where ur going, (didnt read the first post correctly)
Posted: 3.47 Years Ago 

gunni
Germany
Contrib Level: 7
Total Posts: 412
sounds like (manually) overloading a function.
Posted: 3.47 Years Ago 

VBAssassin
United Kingdom
Contrib Level: 17
Total Posts: 5,730
Post Quote - Direct Reference
Haha, yeah, i think your right! It would be overloading... "method overloading"   

i knew there was a name for it!

Kind regards,
Scott
Page 1 of 1
 
 
Latest News About Coder Profile
Coder Profile Poll
If you made money from keeping your profile up to date, say $30 and up, per month. What extra time would you spend on your profile?

No extra time
A few hours at weekends
A whole day each week
Every minute i can get free


please login to cast your vote
and see the results of this poll
Latest Coder Profile Changes
Coder Profile was last updated
3.49 Years Ago
Official Blog :: Make A Donation :: Credits :: Contact Me
Terms & Conditions :: Privacy Policy :: Documents :: Wallpapers
Version 1.46.00
Copyright © 2007 - 2012, Scott Thompson, All Rights Reserved