We help IT Professionals succeed at work.
Get Started

A newbie question regarding php classes

Mark Brady
Mark Brady asked
on
581 Views
Last Modified: 2013-11-10
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.


Comment
Watch Question
Commented:
This problem has been solved!
Unlock 5 Answers and 13 Comments.
See Answers
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE