PHP Developer

My notes and thoughts about Linux, WordPress, PHP and many more.

PHP Syntax of Class, Objects, Methods and Getter/Setter Functions

You should now have a better understanding regarding what are “PHP classes, objects, methods, properties” and how it relates to reality. In this tutorial, you will see what they exactly look like in the PHP script organized in a way even an idiot can understand. Let’s get started:

The most basic PHP object-oriented programming needs at least two files. These are:

a.) index.php (the actual page that would be accessed by the browser or the user)
b.) classes.php (this is the script that contains all of your PHP classes, objects, methods and getter/setter functions).

In the part 1 of this tutorial (Learn PHP Object Oriented Programming), index.php works like the mainscript.php and classes.php works like the sumclass.php for the illustration stated on that part. For simplicity, let’s just call the main script as index.php and the classes as classes.php to prevent any confusion about naming files.

The first part in creating an object oriented program in PHP is to define your classes first before you work on the main script index.php. Here are the steps that you should follow:

1.) Create a new PHP script, save it as classes.php

2.) The first thing that you should do in your classes.php is to define a class. For example, say you need to create a class named as “bicycle”. This is the PHP syntax that you should remember:

It should start with the word “class”, then the name of your class and finally, the open and close braces.

3.) Of course your class is meaningless without objects on it. So you need to add an object to your class. Objects in classes are shown in variables. Variables will describe the object which is called the properties of the object.

How will you describe a bicycle? For example a bicycle can be described in terms of its wheel diameter. So add a property “wheeldiameter” to the class bicycles. This is the php syntax:

The property can be added by using “var” then the PHP variable $name. Make sure your PHP variable is consistent with the property name, if you would like to have a property “$location” then you would use: var $location

4.) Now you have the class, an object with its property name; your class is still not useful because it is not capable of doing something. This is where you will add your methods.

Supposing you will create functions that will set and get the wheel diameter of the bicycle; these functions are called setter and getter functions. For example this is the PHP script:

wheeldiameter = $new_wheeldiameter;  

        function get_wheeldiameter() {
                return $this->wheeldiameter;

It is important that you are consistent with the way how you name the objects and properties in relation to the setter and getter functions. This will make it easy for other programmers to understand your classes, objects, properties and methods/functions.

For example if the property name is wheeldiameter:

The variable property name is $wheeldiameter
The setter function name is set_wheeldiameter
The variable used in setting new wheeldiameter is $new_wheeldiameter
The getter function name is get_wheeldiameter

Let’s have another example, what if you only have a property named as “framesize” instead of “wheeldiameter”? This is how the PHP script would look like with all its objects and functions following the naming conventions:

framesize = $new_framesize;  

        function get_framesize() {
                return $this->framesize;

It’s very easy isn’t it? On the next part you will know how to use you classes.php in the main script index.php.

Featured image credits:

2 thoughts on PHP Syntax of Class, Objects, Methods and Getter/Setter Functions

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> 

This site uses Akismet to reduce spam. Learn how your comment data is processed.