I consider myself an advanced php developer and have been developing large commercial websites for the last 3 years, all without learning PHP's "classes". So far, I have gotten away with writing nice neat code all very well notated and used functions and lots of "includes".
Recently, I decided to dive into learning about OOP (object orientated programming). Although I have much to learn I am getting the hang of it after doing a few easy tutorials. I am however confused by a couple of things. If someone could help make these clearer I would be very happy.
Consider the following class.
<?php
class Bike {
var $Make;
var $Model;
var $Size;
var $Year;
function __construct($Make,$Model,$Size,$Year){
$this->Make = $Make;
$this->Model = $Model;
$this->Size = $Size;
$this->Year = $Year;
}
function get_make() {
return $this->Make;
}
function get_model(){
return $this->Model;
}
function get_size(){
return $this->Size;
}
function get_year(){
return $this->Year;
}
// end of class
}
?>
// I have made this file as small as possible so please ignore the non notation and indents.
In one tutorial, It stated to NEVER access the classes objects directly. Consider this.
<?php
include("bike.class"); // the class I created above
$bike = new Bike("Yamaha","FJ1200","1200cc","2010");
print $bike->Make." ".$bike->Model." ".$bike->Size." ".$bike->Year;
?>
Now why is it bad practice to access the classes objects or variables directly?
$size = $bike->Size;
As opposed to
$size = $bike->get_size();
This line would be shorter to both type, and for the server to read
print $bike->Make." ".$bike->Model." ".$bike->Size." ".$bike->Year;
Than this line
print $bike->get_make()." ".$bike->get_model()." ".$bike->get_size()." ".$bike->get_year();
So if it it much quicker and cleaner looking is it really bad practice to use it like that?
Another question is, if I remove the vars from the class code above (var $Make ...etc) the code still runs fine. So if the code still works without declaring the variables, why do we need to declare them at the top of our class file?
Finally, why is this class so much better than just including a file with a bunch of functions in it?
Any help would be appreciated and also if you know of good class tutorials please post them. Thanks in advance.