Avatar of Mark Brady
Mark BradyFlag for United States of America

asked on 

A newbie question regarding php classes

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.


PHPScripting Languages

Avatar of undefined
Last Comment
Mark Brady
Avatar of Brad Brett
Brad Brett
Flag of United States of America image

You need to take instance from the class to access it using "->", otherwise you will have to create static class and static variables and methods to access it directly using "::" (i.e: myClass::staticVarName).
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Wow, this is a lot of questions!  Much of it is stylistic, as you may have concluded already.  When you create classes that in turn instantiate objects and you expect these to be used by others in programming, "encapsulation" is an important concept.  Your example here uses a constructor to assign the variables and uses either "getter" or direct reference to retrieve the variables.  Does that description of the example offer any hints of the reasoning behind the concept of encapsulation?  

It's not a simple concept but the results become apparent when the designs include large amounts of code and there are a lot of variables.  With little examples the "overhead" of OOP seems significant.  In practice the overhead disappears and the advantages come into focus.
Avatar of Mark Brady
Mark Brady
Flag of United States of America image

ASKER

Thanks to both of you for your replies. I don't follow what you are saying Medo3337. My question really is

1: Why is it unsafe to access a classes variable directly, rather than accessing it through a classes method/function ? ie: $name = $myclass->Name; as opposed to $name = $myclass->get_name();

Ray, what are you talking about "encapsulation"? What has that to do with my question? Sorry but I'm not following you and usually I follow you well! I do understand your final paragraph however.
Avatar of Juan Ocasio
Juan Ocasio
Flag of United States of America image

It's just good programming practice to make your variable inaccess from the outside.  Take for example:

<?php
$MyBike = new Bike("Ford","Explorer","4 door","1996");
echo $MyBike->get_model();
$MyBike->Model = "Expedition" ;
echo $MyBike->get_model();
?>

This will of course echo out

ExplorerExpedition, right?  However  what if later in your code you continue to modify the variable and other variables outside of the class without using the setters and getters?  Eventually your code would be practically unreadable and you won't be able to follow the logic.

Ray's referral to encapsulation has everything to do with the question, because the idea behind oop is just that:  restricting access to some of the classes components, namely the variables, and the only allowing for that access through the use of methods.
Avatar of Mark Brady
Mark Brady
Flag of United States of America image

ASKER

Hi jocasio123:

So what you are saying is if I was NOT going to modify the classes variables then accessing them directly without using the "getters" or functions inside the class would be ok but it is better coding practice to always use the setters and getters? That makes sense. Is that what "encapsulation" is all about encapsulating items inside a class?
SOLUTION
Avatar of Juan Ocasio
Juan Ocasio
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
ASKER CERTIFIED SOLUTION
Avatar of Mr_Wizzy
Mr_Wizzy
Flag of Philippines image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
SOLUTION
Avatar of Zyloch
Zyloch
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
SOLUTION
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

placing function code into a Class does not make it better code

Amen, Slick812!
Avatar of Mark Brady
Mark Brady
Flag of United States of America image

ASKER

I want to thank you ALL very much. Your explanations (all of them) have given me a very clear picture of the advantages of OOP. I went from writing very long scripts to placing lots of my code in functions and including large blocks of code when/if needed and that really tidied up my code and made debugging so much easier and faster. Always on the lookout for knowledge I was considering if I should make the effort to learn OOP and now I have read your explanations on the various aspects of OOP, I have decided to jump in feet first!.

I only wish I had learned OOP before a huge project I have just finished for a big client. Debugging that one takes a big chunk of my day. Once I am comfortable with writing classes/constructors and getters/setters I will use it with the next project I get. Thank you all so much. Once again, I knew I could rely on my friends at EE. I have help many many people (like most of you have) on EE and don't ask to many questions myself but I have just spent my 3 days vacation time reading tutorials on OOP with PHP and I must say I was little more than none the wiser. I had many questions as the tuts I read were poorly written in my opinion.

This thread has changed that and I would recommend any newbie to OOP to read this to gain insite.
Thank you thank you thank you. Everyone deserves the points but I'll split them. Have a great day/night from New Zealand!
Avatar of Mark Brady
Mark Brady
Flag of United States of America image

ASKER

I'm am very happy to now be able to move into OOP with
PHP and expand my knowledge of this wonderful language. EE is by far the best place to learn and we NEVER EVER stop learning.
PHP
PHP

PHP is a widely-used server-side scripting language especially suited for web development, powering tens of millions of sites from Facebook to personal WordPress blogs. PHP is often paired with the MySQL relational database, but includes support for most other mainstream databases. By utilizing different Server APIs, PHP can work on many different web servers as a server-side scripting language.

125K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo