Programming Theory
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Codewalkers ForumsOther TechnologiesProgramming Theory

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Codewalkers Forums Sponsor:
  #1  
Old June 11th, 2005, 02:42 AM
tasoua tasoua is offline
Contributing User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 103 tasoua User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 50 m 19 sec
Reputation Power: 2
Polymorphism and using switch

Hello firends

From this site:https://www.entwickler.com/itr/online_artikel/psecom,id,284,nodeid,114.html






The usage of switch() in an OO situation is hardly ever needed, and even if it is, you might want to reconsider it. Using switch() to determine what to do with an object can become a maintenance nightmare. If you use switch() to determine of which type an object is, after which you are going to do things with the object in every different case, requires that you update your switch() statement every time a new object is added to your application. We do not want that, do we?





And this is example about polymorphism from this site:





php Code:
Original - php Code
  1.  
  2. <?php
  3. // note: make sure the definitions of the form element classes are available here
  4.  
  5. // instantiate some form elements
  6. $myTextfield = new TextFormElement(
  7. "myTextfield",
  8. "My Textfield",
  9. "a value",
  10. 20
  11. );
  12. $myCheckbox = new CheckboxFormElement(
  13. "myCheckbox",
  14. "My Checkbox",
  15. "a value",
  16. true
  17. );
  18. $mySecondCheckbox = new CheckboxFormElement(
  19. "mySecondCheckbox",
  20. "My Second Checkbox",
  21. "a value",
  22. true
  23. );
  24.  
  25. // make an array of all the elements so we can loop through them
  26. $objects = array($myTextfield, $myCheckbox, $mySecondCheckbox);
  27.  
  28. foreach ($objects as $object) {
  29. //see of what type the current object is
  30. switch (get_class($object)) {
  31. case "textformelement":
  32. // print html code for current text element here
  33. break;
  34. case "checkboxformelement":
  35. // print html code for current checkbox element here
  36. break;
  37. default:
  38. break;
  39. }
  40. }
  41. ?>
  42.  
  43.  





This is from PHP cook book oreilly:






PHP doesn't support method polymorphism as a built-in feature. However, you can emulate it using various type-checking functions







Because PHP doesn't allow you to declare a variable's type in a method prototype, it can't conditionally execute a different method based on the method's signature, as can Java and C++. You can, instead, make one function and use a switch statement to manually recreate this feature.




And its example:



php Code:
Original - php Code
  1.  
  2. class pc_Image {
  3.  
  4. var $handle;
  5.  
  6. function ImageCreate($image) {
  7. if (is_string($image)) {
  8. // simple file type guessing
  9.  
  10. // grab file suffix
  11. $info = pathinfo($image);
  12. $extension = strtolower($info['extension']);
  13. switch ($extension) {
  14. case 'jpg':
  15. case 'jpeg':
  16. $this->handle = ImageCreateFromJPEG($image);
  17. break;
  18. case 'png':
  19. $this->handle = ImageCreateFromPNG($image);
  20. break;
  21. default:
  22. die('Images must be JPEGs or PNGs.');
  23. }
  24. } elseif (is_resource($image)) {
  25. $this->handle = $image;
  26. } else {
  27. die('Variables must be strings or resources.');
  28. }
  29. }
  30. }
  31.  
  32.  




From that site:

https://www.entwickler.com/itr/online_artikel/psecom,id,284,nodeid,114.html





Using switch() to determine what to do with an object can become a maintenance nightmare.




But PHP cook book has used switch()

Which one is right?

Reply With Quote
  #2  
Old June 27th, 2005, 03:48 AM
zombie zombie is offline
Codewalkers Intermediate (1500 - 1999 posts)
 
Join Date: Apr 2007
Location: serbia
Posts: 1,876 zombie User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 3
RE: Polymorphism and using switch

Quote:
PHP doesn't support method polymorphism as a built-in feature. However, you can emulate it using various type-checking functions


this is simply not true.. php does in fact support polymorphism, so you shouldn't use switch statement..

Reply With Quote
Reply

Viewing: Codewalkers ForumsOther TechnologiesProgramming Theory > Polymorphism and using switch


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway