Classes in PHP

PHP has classes for the purpose of abstraction and encapsulation. Class is a collection of variables and functions which performs similar task. Classes help in security issues by separating interface from implementation. These add more restriction to functions and variables so that other can not access them easily. Class is defined with class keyword following with its name. For code standard purposes, class name starts with a capital letter. A simple syntax of class is:

  1. <?php
  2. /**
  3. * this is a simple class named ClassName
  4. * A capital letter is used instead of '_' for space
  5. */
  6. class ClassName
  7. {
  8. // code
  9. }
  10. ?>

The code inside the class can not be executed directly. First we have to make object of that class. Object is instance of that class which has all variables and functions of that class as its properties.

We create an object like this: object_name = ClassName();

For calling a function of class, pointer '->' is used. (object_name->method_name();)

  1. <?php
  2. class ClassName
  3. {
  4. // defining class variable
  5. // can only assign simple values during initialisation
  6. var $class_variable;
  7. //constructor of the class
  8. # equivalent to => function __construct(){}
  9. function ClassName()
  10. {
  11. $this->class_variable = 60 * 60;
  12. echo "it is constructor
  13. ";
  14. }
  15. // returning class variable
  16. function get_global()
  17. {
  18. return $this->class_variable;
  19. }
  20. // modifying class variable
  21. function set_global($value)
  22. {
  23. $this->class_variable = $value;
  24. }
  25. // displaying class variable
  26. public function reset_display()
  27. {
  28. $this->private_function();
  29. echo $this->get_global()."
  30. ";
  31. }
  32. // private function
  33. private function private_function()
  34. {
  35. $this->class_variable = 60 * 60;
  36. }
  37. }
  38. $object_name = new ClassName();
  39. echo $object_name->get_global()."
  40. ";
  41. $object_name->set_global(231);
  42. echo $object_name->get_global()."
  43. ";
  44. $object_name->reset_display();
  45. ?>

In above example, you can see following concepts:

  1. A variable which is accessible in whole class ($class_variable) is defined with keyword 'var'. Local variables are defined inside the functions and can be accessed only from those functions.
  2. Class variables can only assign constant values (2, 4, 'string', etc) during the definition time. We can not define a class variable like this => var $variable = 60 *60; because 60*60 is a mathematics expression, not a constant.
  3. Class variable and functions are accessed in the class using '$this->variable_name;' and '$this->function_name();' respectively. $this is the pointer of the same class.
  4. Constructor is a special function in the class. It is used to initialize class variables. This function run when we create object of that class. We can also send arguments to the constructor.
  5. We can define a constructor in two ways: first by giving it name same as class name and second by giving it name as __construct.
  6. There are three types of access identifiers: public, private and protected. By default functions of the class are public. Public keyword means method is open for whole world and who can access this class, can access that function also. Private means that function is accessible only inside the class and no one can access that function from outside that class. Protected means that function can be access only from that class and classes extending that class. It is concept of inheritance which we will read in next tutorial.

Output of above code is:

  1. it is constructor
  2. 3600
  3. 231
  4. 3600

Click on banner below to Learn: PHP with Laravel for beginners - Become a Master in Laravel

About Harish Kumar

Harish, a technical core team member at with five year experience in full stack web and mobile development, spends most of his time on coding, reading, analysing and curiously following businesses environments. He is a non-graduate alumni from IIT Roorkee, Computer Science and frequently writes on both technical and business topics.

Related Articles

PHP stands for PHP: Hypertext Preprocessor. PHP is known as a server side scripting and interpreter language for web ...
Introduction to PHP
In this tutorial, we will study about conditional statements (if else statements and switch statements). These statem...
Conditional statements in php
In this tutorial, we will study about strings and operations upon strings. Due to long length of this section, I have...
Strings in PHP (part-1)

Full-Stack Web Development with React

Best Udemy Courses

Top Posts

Recent Posts

The Complete Web Developer Course - Build 25 Websites


Subscribe now and get weekly updates directly in your inbox!

Any Course on Udemy in $6 (INR 455)

Development Category (English)300x250