troubleshooting Question

A newbie question regarding php classes

Avatar of Mark Brady
Mark BradyFlag for United States of America asked on
PHPScripting Languages
13 Comments5 Solutions585 ViewsLast Modified:
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.

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.

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.


Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 5 Answers and 13 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 5 Answers and 13 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros