InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

Menu
  • Home
  • Sitemap

Python Programming Language Best Tutorials and Code Examples

Learn Python Right Now!
Home
PHP
Using Method Polymorphism in PHP
PHP

Using Method Polymorphism in PHP

InfinityCoder November 25, 2016

You want to execute different code depending on the number and type of arguments passed to a method.

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

The following combine() function uses is_numeric(), is_string(), is_array(), and is_bool():

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// combine() adds numbers, concatenates strings, merges arrays,
// and ANDs bitwise and boolean arguments
function combine($a, $b) {
   if (is_int($a) && is_int($b)) {
       return $a + $b;
   }
 
   if (is_float($a) && is_float($b)) {
       return $a + $b;
   }
 
   if (is_string($a) && is_string($b)) {
       return "$a$b";
   }
 
   if (is_array($a) && is_array($b)) {
       return array_merge($a, $b);
   }
 
   if (is_bool($a) && is_bool($b)) {
       return $a & $b;
   }
 
   return false;
}

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 Java and C++ can.

You can, instead, make one function and use a switch statement to manually re-create this feature.
For example, PHP lets you edit images using GD. It can be handy in an image class to be able to pass in either the location of the image (remote or local) or the handle PHP has assigned to an existing image stream.

This Image class that does just that:

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

In this case, any string passed in is treated as the location of a file, so use pathinfo() to grab the file extension.

Once you know the extension, try to guess which ImageCreate From() function accurately opens the image and create a handle.
If it’s not a string, you’re dealing directly with a GD stream, which is a type of re source.

Because there’s no conversion necessary, assign the stream directly to $han dle. Of course, if you’re using this class in a production environment, you’d be more robust in your error handling.

Method polymorphism also encompasses methods with differing numbers of arguments.
The code to find the number of arguments inside a method is identical to how you process variable argument functions using func_num_args().

Share
Tweet
Email
Prev Article
Next Article

Related Articles

Generating Predictable Random Numbers in PHP
You want to make the random number generate predictable numbers …

Generating Predictable Random Numbers in PHP

Fetching a URL with the GET Method in PHP
You want to retrieve the contents of a URL. For …

Fetching a URL with the GET Method in PHP

About The Author

InfinityCoder
InfinityCoder

Leave a Reply

Cancel reply

Recent Tutorials InfinityQuest

  • Adding New Features to bash Using Loadable Built-ins in bash
    Adding New Features to bash Using Loadable …
    June 27, 2017 0
  • Getting to the Bottom of Things in bash
    Getting to the Bottom of Things in …
    June 27, 2017 0

Recent Comments

  • fer on Turning a Dictionary into XML in Python
  • mahesh on Turning a Dictionary into XML in Python

Categories

  • Bash
  • PHP
  • Python
  • Uncategorized

InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

About Us

Start learning your desired programming language with InfinityQuest.com.

On our website you can access any tutorial that you want with video and code examples.

We are very happy and honored that InfinityQuest.com has been listed as a recommended learning website for students.

Popular Tags

binary data python CIDR convert string into datetime python create xml from dict python dictionary into xml python how to create xml with dict in Python how to write binary data in Python IP Address read binary data python tutorial string as date object python string to datetime python

Archives

  • June 2017
  • April 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
Copyright © 2021 InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more
Programming Tutorials | Sitemap