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:-
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...).PHP Code:
<?php namespace GodOnly; class Human{ } ?>
So let's define a few of those.
Okay! Great! I've got my class. How do I use it?PHP Code:
<?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... */ } ?>
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).
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 justPHP Code:
<?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! ?>
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.
Changes to "awesome_god_like_human_maker.php":PHP Code:
<?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. ?>
function createBrain($iq_score){ /* $iq_score will equal whatever we pass to it when we call that function */ }