PHP Free Tutorial

Creating Class in PHP

In OOP, PHP, Programming by Baqir Ali

What is Class In PHP?

Class is a blueprint of objects or a model for objects with a fixed number of components that are called members of the class therefore to create a class follow the following steps.

  • Create a new file called index.php
  • Create a class using the ‘Class’ keyword
<?php
	class myClass
	{
		//members of the class
	}
?>

Members of the class can be class variables, constructors, and methods. Members of the class can be categorized into three categories.

  • Public: accessible from anywhere
  • Private: accessible from within the same class
  • Protected: accessible from the same package

The above method is also known as Encapsulation. Encapsulation is just a protection mechanism either class data members should be directly or indirectly accessible from outside the class.

<?php
	class Student
	{
		public $id;
		public $name;
		public $class;
		
	}

?>

If the class members are public they are easily accessible outside the class therefore we can access class members outside the class using class objects. As the class is only a blueprint we are not able to use the class unless we create the object.

<?php
	class Student
	{
		public $id;
		public $name;
		public $class;
	}

	$obj=new Student();
	$obj->id=10;
	$obj->name='Elia Haider';
	$obj->class='7th';


	echo "<br /> His ID is ".$obj->id;
	echo "<br /> His Name is ".$obj->name;
	echo "<br /> He is in Class ".$obj->class;
?>

Member Access Operator: (->) is a member access Operator

Output:

His ID is 10
His Name is Elia Haider
He is in Class 7th

Constructor in PHP

Constructors are used to initializing class variables so Whenever an object is created class constructor is called. When an object without a parameter is created there is no need to create a class constructor as the default constructor is called. For an object with parameters, the constructor is a must.

<?php
	class Student
	{
		public $id;
		public $name;
		public $class;
		
		function __construct($id,$name,$class)
		{
			$this->id=$id;
			$this->name=$name;
			$this->class=$class;
		}
	}

	$obj=new Student(100,'Dabeer Haider','9th');

	echo "<br /> Your ID is ".$obj->id;
	echo "<br /> Your Name is ".$obj->name;
	echo "<br /> Your Class is ".$obj->class;
?>

Output:


Your ID is 100
Your Name is Dabeer Haider
Your Class is 9th

Function in PHP Class

What if we define private the class members how do we print the values outside the class. We define a public function in the class that can print the class variable values.

<?php
	class Student
	{
		private $id;
		private $name;
		private $class;
		
		function __construct($id,$name,$class)
		{
			$this->id=$id;
			$this->name=$name;
			$this->class=$class;
		}


		function display()
		{
			echo "<b>This is Display Function</b>";
			echo "<br /> Your ID is ".$this->id;
			echo "<br /> Your Name is ".$this->name;
			echo "<br /> Your Class is ".$this->class;
		}
	}

	$obj=new Student(10,'Elia Haider','7th');
	$obj->display();
	
?>

Output:

This is Display Function
Your ID is 10
Your Name is Elia Haider
Your Class is 7th

Support us by sharing this post