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 */ }
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 */ }