Class in PHP
A class is a blueprint from which you can create the instance, i.e., objects. A class is used to bind data as well as methods together as a single unit. A class doesn’t take any memory spaces when a programmer creates one. It represents the set of properties or methods that are common to all objects of one type. One of the big differences between functions and classes is that a class contains both data (variables) and functions that form a package called an: ‘object’. Class and Objects in PHP
simple PHP class
You define your own class by starting with the keyword ‘class’ followed by the name you want to give your new class. You enclose a class using curly braces ( { } ) just like you do with functions.
<?php class class_name { } ?>
Add data to class
When you create a variable inside a class, it is called a ‘property’.
<?php class class_name { var $data; } ?>
Add functions/methods to your class
A class’s methods are used to manipulate its own data / properties.
<?php class addition { var $data; function add($num1, $num2) { $sum = $num1 + $num2; return $sum; } } ?>
Getter and setter functions
These methods follow a common OOP convention that you see in many languages where you create methods to ‘set’ and ‘get’ properties in a class. Another convention is that getter and setter names should match the property names. This way, when other PHP programmers want to use your objects, they will know that if you have a method/function called ‘set_name()’, there will be a property/variable called ‘name’. Class and Objects in PHP
<?php class person { var $name; function set_name($new_name) { $this->name = $new_name; } function get_name() { return $this->name; } } ?>
Creating Objects in PHP
Once you defined your class, then you can create as many objects as you like of that class type. Following is an example of how to create object using new operator.
<?php class person { var $name; function set_name($new_name) { $this->name = $new_name; } function get_name() { return $this->name; } } $person1 =new person; ?>
Calling Member Functions
After creating your objects, you will be able to call member functions related to that object. One member function will be able to process member variable of related object only.
<?php class person { var $name; function set_name($new_name) { $this->name = $new_name; } function get_name() { return $this->name; } } $person1 =new person; $person1->set_name("tycheVentures"); echo "$person1->get_name()"; ?>
This will print the following result
tycheVentures
Access Specifiers in PHP
There are three types of Access Specifiers available in PHP, Private, Protected and Public.
Public: Class members declared public can be accessed everywhere.
Protected: Class members declared protected can be accessed only within the class itself and by inheriting classes.
Private: Class members declared as private may only be accessed by the class that defines the member.
Specifier | Access within same class | Access in derived class | Access outside class |
---|---|---|---|
Private | Yes | No | No |
Public | Yes | Yes | Yes |
Protected | Yes | Yes | No |