I’m pretty proficient with PHP, outside of OOP – which I am just now starting to jump in to.
I’ve been watching videos and reading tutorials, but they are all still pretty confusing…
If I have
FILE 1 (class.time.php)
class Time {
function GetTime(){
$time = date('H:i:s');
printf($time);
}
}
and then in a nother php page I’ve got
FILE 2 (page.php)
I can do
include('class.time.php');
and then anywhere in this page I can then do
$time = new Time; //Calling the class and setting the class to a variable
$time->GetTime(); //This is BASICALLY saying (run the 'GetTime' function in the 'Time Class'
My main question is, is the comment above (This is BASICALLY saying…..) correct? or is there a better way to think of it?
regarding method calls
I would make one correction to your statement, hinting a little more accurately at what Object Orientation is all about. Your comment reads:
Which is only kinda accurate, and expounding on it may clarify OOP for you. For the purposes of this post, I might rephrase it this way:
See the difference? You aren’t just calling a function from a class, you are saying “get the processing instructions from the class, but bind those instructions to this specific object and then execute.
It may sound like mincing words, but it’s critical to understanding encapsulation. Objects of a class share their method definitions, but they do not share data.
You can think of an object as essentially a set of two things:
(the “special” nature of the methods’ relationship to the data is either implicit or explicit, it’s up to you)
The distinction is readily evident when you start using “member variables” (sometimes called “object properties”, “instance variables (aka ivars)”, or similar names). Consider this sample code:
This highlights the fact that each object has it’s own memory for instance variables. They share the definition of those variables, as well as the blocks of code written to process those variables.
The special variable
$thisis of particular interest to my overall intention with this answer: the same variable in the same method definition actually points to a different object depending on which object you call the method on.regarding
newNow to the previous line, where your comment reads (in regard to
$time = new Time):The way I would phrase it is:
That word, instance will be a big concept to get solid before moving on. You don’t call a class, you call a method, and you don’t (in PHP at least) set the value of variables to be a class, you set them to point to instances of the class. Sometimes people use the words instance and object interchangeably, which is fine. But you should not use them interchangeably with the word class.
Let’s get technical:
When you use the
newoperator, technically you are telling PHP to do two things:So if you call
newtwice, you grab enough memory for 2 objects, and so on. PHP uses your class definition to figure out how much memory to allocate for you, and find the special method to call.That special method is called
__construct, and is guaranteed to be called when your objects are created, with that special$thisvariable pointing to your shiny new memory. Here’s a sample of how you might use it:Even though you don’t see it,
__construct()was called and the value of$createdTimewas initialized.