A lot of PHP programmers tend to program their scripts the 'old' style, which is procedural. There's nothing wrong about this, in fact, this is perfect for small PHP programs, but Object Oriented can offer a lot to programmers writing big scripts. I'm not going to go through the advantages / disadvantages etc as I just want to teach you OOP, but just make sure your projects need OOP before actually using OOP in your project as OOP can sometimes use up more programming than procedural in small projects.
Objects are basically the building blocks of a program. Think of objects as bricks - they all join together to make a whole building, in our case, a software or program.
Before you can create objects, you need to design a blueprint (what the object is made from). To do this, we make a class. To make this easier, I'm going to call the class 'myClass' and relate to it as a class in school, and the functions inside the class are going to be what you find in a class. Inside a class we can have global variables, functiosn etc, functions being the neccessity for OOP to work.
Syntax for my example:
<?php class myClass { function students() { } function books() { } } ?>
I'm not going to write anything in the functions as I just want to show you how they work. So basically, we have functions inside the class. They work just like functions, can return things, take parameters etc.
You can put a statement into the function such as:
function students() { $student_one = "Jimmy Smith"; $student_two = "Michael Dane"; $student_three = "Ashley Wotsits"; echo "We have the following students: $student_one, $student_two, $student_three in our class."; }
To call the function, OUTSIDE THE CLASS, we have to create a NEW object (I will call it myObject) based on the blueprint (the class called myClass):
Quote
$myObject = new myClass();
// Now we have to call the function inside myClass
$myObject -> students();
// This should run the function called students in myClass and output what we put in the function.
// Now we have to call the function inside myClass
$myObject -> students();
// This should run the function called students in myClass and output what we put in the function.
I hope this helps!
Thanks!