![]() |
|
Introduction to class polymorphism - Printable Version +- Sinisterly (https://sinister.li) +-- Forum: Coding (https://sinister.li/Forum-Coding) +--- Forum: PHP (https://sinister.li/Forum-PHP) +--- Thread: Introduction to class polymorphism (/Thread-Introduction-to-class-polymorphism) |
Introduction to class polymorphism - 1llusion - 07-21-2013 Hello, I'll introduce you to class polymorphism today. This will be just a brief introduction and should give you an idea of what polymorphism is. What is class polymorphism and where to use it? If I put it simply, polymorphism lets you use classes with different functionality the same way. It can be used instead of huge "If... elseif..." blocks and make your code flow a lot smoother and cleaner. With polymorphism, your code won't need to know with which class it is working, because they will all be used the same way. Examples: Let's imagine an application, where we have certain animals and other beings and they talk depending on what the user requests. The users choice is sent through GET request under the parameter "id". Many users would probably do something like this: PHP Code: if($_GET['id'] == 'dog'){
echo "Woof";
}
elseif($_GET['id'] == 'cat'){
echo "Meow";
}
elseif($_GET['id'] == 'robot'){
echo "The current time is: ".date("H:i:s", time());
}
You can clearly see there what each input does. However, if you want to add extra animals or anything else, it can get pretty long and the code will be unreadable. Due to these facts, you might go for a switch: PHP Code: switch($_GET['id']){
case 'dog':
echo "Woof";
break;
case 'cat':
echo "Meow";
break;
case 'robot':
echo "The current time is: ".date("H:i:s", time());
break;
}
This looks better. The code is cleaner and you can see what is going on. However, the statement still can get pretty long when you add additional animals etc. and if complicated code is needed, it would make the statement un-readable and hard to maintain. So how do we solve our problem? So we can have an easy to maintain code with possibility to extend it? Look at the following code: PHP Code: class dog{
public function talk(){
echo "Woof";
}
}
class cat{
public function talk(){
echo "Meow";
}
}
class robot{
public function talk(){
echo "The current time is: {$this->time()}";
}
private function time(){
return date("H:i:s", time());
}
}
$class = new $_GET['id'];
$class->talk();
For purpose of this tutorial, I included all of the classes in one file, you might want to put each class into a separate file and include it. If you take out all of the classes, your code has suddenly shrunken into 2 lines: PHP Code: $class = new $_GET['id'];
$class->talk();
these two lines have the same functionality as the above examples using switch and If. So how does it work? You create a class with a common interface. I didn't define an interface here just for the sake of keeping the tutorial short (interfaces require their own tutorial), but I strongly suggest you to do so. Our classes share the same function called "talk()". This function will echo back the result of what the animal does. Now when you have these classes ready, you can start calling them. First we create an instance of the class, where the class name is the users input (here I suggest to make sure, that the class exists, for example by using: class_exists() function) and then we simply call the function "talk()". Simple as that. Now we don't have to care what was the users input, we save time on finding it out and we execute the right option straight away! To add new animals etc., you just need to make a new class, no need to edit any of the existing code as long as the new class uses the same interface. For more in-depth tutorial, visit: http://net.tutsplus.com/tutorials/php/understanding-and-applying-polymorphism-in-php/ If you have any questions, I'll be happy to answer them
Introduction to class polymorphism - 1llusion - 07-21-2013 Hello, I'll introduce you to class polymorphism today. This will be just a brief introduction and should give you an idea of what polymorphism is. What is class polymorphism and where to use it? If I put it simply, polymorphism lets you use classes with different functionality the same way. It can be used instead of huge "If... elseif..." blocks and make your code flow a lot smoother and cleaner. With polymorphism, your code won't need to know with which class it is working, because they will all be used the same way. Examples: Let's imagine an application, where we have certain animals and other beings and they talk depending on what the user requests. The users choice is sent through GET request under the parameter "id". Many users would probably do something like this: PHP Code: if($_GET['id'] == 'dog'){
echo "Woof";
}
elseif($_GET['id'] == 'cat'){
echo "Meow";
}
elseif($_GET['id'] == 'robot'){
echo "The current time is: ".date("H:i:s", time());
}
You can clearly see there what each input does. However, if you want to add extra animals or anything else, it can get pretty long and the code will be unreadable. Due to these facts, you might go for a switch: PHP Code: switch($_GET['id']){
case 'dog':
echo "Woof";
break;
case 'cat':
echo "Meow";
break;
case 'robot':
echo "The current time is: ".date("H:i:s", time());
break;
}
This looks better. The code is cleaner and you can see what is going on. However, the statement still can get pretty long when you add additional animals etc. and if complicated code is needed, it would make the statement un-readable and hard to maintain. So how do we solve our problem? So we can have an easy to maintain code with possibility to extend it? Look at the following code: PHP Code: class dog{
public function talk(){
echo "Woof";
}
}
class cat{
public function talk(){
echo "Meow";
}
}
class robot{
public function talk(){
echo "The current time is: {$this->time()}";
}
private function time(){
return date("H:i:s", time());
}
}
$class = new $_GET['id'];
$class->talk();
For purpose of this tutorial, I included all of the classes in one file, you might want to put each class into a separate file and include it. If you take out all of the classes, your code has suddenly shrunken into 2 lines: PHP Code: $class = new $_GET['id'];
$class->talk();
these two lines have the same functionality as the above examples using switch and If. So how does it work? You create a class with a common interface. I didn't define an interface here just for the sake of keeping the tutorial short (interfaces require their own tutorial), but I strongly suggest you to do so. Our classes share the same function called "talk()". This function will echo back the result of what the animal does. Now when you have these classes ready, you can start calling them. First we create an instance of the class, where the class name is the users input (here I suggest to make sure, that the class exists, for example by using: class_exists() function) and then we simply call the function "talk()". Simple as that. Now we don't have to care what was the users input, we save time on finding it out and we execute the right option straight away! To add new animals etc., you just need to make a new class, no need to edit any of the existing code as long as the new class uses the same interface. For more in-depth tutorial, visit: http://net.tutsplus.com/tutorials/php/understanding-and-applying-polymorphism-in-php/ If you have any questions, I'll be happy to answer them
Introduction to class polymorphism - 1llusion - 07-21-2013 Hello, I'll introduce you to class polymorphism today. This will be just a brief introduction and should give you an idea of what polymorphism is. What is class polymorphism and where to use it? If I put it simply, polymorphism lets you use classes with different functionality the same way. It can be used instead of huge "If... elseif..." blocks and make your code flow a lot smoother and cleaner. With polymorphism, your code won't need to know with which class it is working, because they will all be used the same way. Examples: Let's imagine an application, where we have certain animals and other beings and they talk depending on what the user requests. The users choice is sent through GET request under the parameter "id". Many users would probably do something like this: PHP Code: if($_GET['id'] == 'dog'){
echo "Woof";
}
elseif($_GET['id'] == 'cat'){
echo "Meow";
}
elseif($_GET['id'] == 'robot'){
echo "The current time is: ".date("H:i:s", time());
}
You can clearly see there what each input does. However, if you want to add extra animals or anything else, it can get pretty long and the code will be unreadable. Due to these facts, you might go for a switch: PHP Code: switch($_GET['id']){
case 'dog':
echo "Woof";
break;
case 'cat':
echo "Meow";
break;
case 'robot':
echo "The current time is: ".date("H:i:s", time());
break;
}
This looks better. The code is cleaner and you can see what is going on. However, the statement still can get pretty long when you add additional animals etc. and if complicated code is needed, it would make the statement un-readable and hard to maintain. So how do we solve our problem? So we can have an easy to maintain code with possibility to extend it? Look at the following code: PHP Code: class dog{
public function talk(){
echo "Woof";
}
}
class cat{
public function talk(){
echo "Meow";
}
}
class robot{
public function talk(){
echo "The current time is: {$this->time()}";
}
private function time(){
return date("H:i:s", time());
}
}
$class = new $_GET['id'];
$class->talk();
For purpose of this tutorial, I included all of the classes in one file, you might want to put each class into a separate file and include it. If you take out all of the classes, your code has suddenly shrunken into 2 lines: PHP Code: $class = new $_GET['id'];
$class->talk();
these two lines have the same functionality as the above examples using switch and If. So how does it work? You create a class with a common interface. I didn't define an interface here just for the sake of keeping the tutorial short (interfaces require their own tutorial), but I strongly suggest you to do so. Our classes share the same function called "talk()". This function will echo back the result of what the animal does. Now when you have these classes ready, you can start calling them. First we create an instance of the class, where the class name is the users input (here I suggest to make sure, that the class exists, for example by using: class_exists() function) and then we simply call the function "talk()". Simple as that. Now we don't have to care what was the users input, we save time on finding it out and we execute the right option straight away! To add new animals etc., you just need to make a new class, no need to edit any of the existing code as long as the new class uses the same interface. For more in-depth tutorial, visit: http://net.tutsplus.com/tutorials/php/understanding-and-applying-polymorphism-in-php/ If you have any questions, I'll be happy to answer them
RE: Introduction to class polymorphism - xevenofhearts - 07-21-2013 Easy to understand tutorial, I learned a lot from it. Great work
RE: Introduction to class polymorphism - xevenofhearts - 07-21-2013 Easy to understand tutorial, I learned a lot from it. Great work
RE: Introduction to class polymorphism - xevenofhearts - 07-21-2013 Easy to understand tutorial, I learned a lot from it. Great work
RE: Introduction to class polymorphism - The Alchemist - 07-22-2013 Why are interfaces recommended? Why not classes? The job is getting done pretty well with classes. RE: Introduction to class polymorphism - The Alchemist - 07-22-2013 Why are interfaces recommended? Why not classes? The job is getting done pretty well with classes. RE: Introduction to class polymorphism - The Alchemist - 07-22-2013 Why are interfaces recommended? Why not classes? The job is getting done pretty well with classes. RE: Introduction to class polymorphism - 1llusion - 07-22-2013 (07-22-2013, 01:35 AM)The Alchemist Wrote: Why are interfaces recommended? Why not classes? Huh? I mean implementing interfaces into classes such as: PHP Code: class foo implements bar{
// code
}
Interfaces don't have any functionality on their own
|