Basic Class in Object Oriented Programming PHP

To create a new data type, you can use the CLASS keyword followed by the class name, where the class name must be a string. The class name must not be the same, with the name of the keyword in the PHP library. It should be noted that different from the the object class, where class is a new data type while the object is an instance / example of a utilization data type.

Basic Class in Object Oriented Programming PHP

Consider the following class declaration

Where class is a keyword that must be used when creating a new class, class_name is name of the class. Then after the class name created,please use curly brackets { , and ends with a closing curly brackets }.

Class is a new data type and consists of several data and functions. consider the following example


Classes are created by default which has the data and functions that are global or public, where all objects of other classes can access these functions. In PHP, there are 5 important keywords ie

1. public : If you use this keyword then, data or functions can be accessed by the object from the outside
2. private : Allow data and functions accessible from within the class itself. Data or function with the keyword private inaccessible other classes or subclasses
3. protected : Allow data or functions, accessed by the class itself or subclasses
4. final : Used to prevent the function from the parent can not be redefined in the derived function.
5. abstract : This function must be declared back in derivatives function, which is inverse with final


The important pointer to note is $this . $this should be used if you will access / use existing data member in the class. If you do not use $this at the time of accessing the data member, it will cause a fatal error.
$this works to reference the object caller, so the data or functions that will be called from the new data type will be returned to the caller object precisely, consider the following example

Important :
If you accessing data such as data $age, it can use the keyword $this with $this->age. Note the $age residing in the variable, when accessed premises $this keyword, then the $ sign must be removed on the variable name


After creating a new data type, then you can create an object based on that type. To create an object of a new data type, or class then use the new keyword followed by the name of the class referred. Consider the following script

The first script is used to create objects pussy with data type Cat (), while the second line we create objects pussy with data type Cat () followed by the parameters age and name.


After creating objects using a new data type that we can access all the components that are in the class. To access the functions that exist in the new data type then use the object name followed by the name of the function using the arrow.

How ? easy right ? article above is the basic foundation of making a php class using object oriented programming. Before you learn this session, you can read my article before about Concept Object Oriented Programming PHP for beginners

The following two tabs change content below.
This site is a personal Blog of Sigit Prasetya Nugroho, a Desktop developer and freelance web developer working in PHP, MySQL, WordPress.

Leave a Comment

Your email address will not be published.

Time limit is exhausted. Please reload CAPTCHA.

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