PDA

View Full Version : [Tutorial][PHP] An introduction to OOP.



joshua06860
04-09-2014, 08:00 AM
Hey guys,


Just here to give a quick intro into PHP OOP (Object-Oriented Programming).
Now, a way I like to look at this is by using a person as an example.
Every person is a human being. So, if we were using PHP to create humans (though, I would hope we weren't created by PHP) I may have a class called Human - which may look something like this:-


<?php namespace GodOnly; class Human{ } ?>Now, inside the Human class I will probably have a few different functions. One for creating all the necessary things the human needs to live (heart etc...).
So let's define a few of those.



<?php namespace GodOnly; class Human{ function createHair(){ /* an awesome god-like function to create hair */ } function createEyes(){ /* an awesome god-like function to create eyes */ } function createBrain(){ /* an awesome god-like function to create brains */ } /* etc... */ } ?>

Okay! Great! I've got my class. How do I use it?
Well, first of all you need to create an instance of that class and assign it to a variable.
Um, what? Just do as I do below (in this example I include my class into the file in which I want to use those functions).


<?php require_once("awesome_god_like_human_maker.php"); /* some more god-like code created by a god-like person (me) */ $human_maker = new \GodOnly\Human(); //Create a new instance of our class $human_maker->createBrain(); //Initiate our awesome brain-making function! ?>

Now, if we're going to be creating thousands of humans with different attributes we're not going to want to go through this code over and over again just
to change a few things like eye color, hair color and intellect. Well, it's a bloody good job we don't have to because we can pass information to those functions just as we would in procedural.



<?php require_once("awesome_god_like_human_maker.php"); /* some more god-like code created by a god-like person (me) */ $human_maker = new \GodOnly\Human(); $human_maker->createBrain(98); //Here I've passed along an expected IQ score to that brain. ?>
Changes to "awesome_god_like_human_maker.php":
function createBrain($iq_score){ /* $iq_score will equal whatever we pass to it when we call that function */ }

Richie
05-09-2014, 11:59 AM
Instructions were not clear enough, i got my **** stuck in the toaster.

Edited by e5 (Forum Super Moderator) - Please do not post pointessly.

Chippiewill
05-09-2014, 12:19 PM
Interesting tutorial, it's a little hard to follow and I suspect only those who already understand OOP will understand the tutorial. A Very large critique from me is that without the use of line-breaks the code is completely incomprehensible. Cool none the less.

joshua06860
05-09-2014, 04:18 PM
Sorry about that, the PHP tags seem to have broken my line breaks and tabs.

Trinity
06-09-2014, 10:37 PM
I agree with Chippiewill, I was going to say the same things.
It needs a bit more work, perhaps a proper introduction, then the tutorial, then a small conclusion just to give it some structure. It doesn't really explain what OOP is and why it's used and whatnot, but I like the way it's written and this forum could do with some more tutorials and other things to help beginners, so I like it :)

redtom
20-09-2014, 10:49 AM
I think its great that your contributing content to the community but as people have said the structure of the formatting kind of knocks it off a little. Having said that I've gotta give it to you that it's probably the simplest way I've seen OOP with PHP explained a lot of people seem to make out that its hard to understand.

Want to hide these adverts? Register an account for free!