Class in php with example

php user class

What is Class

class is a blueprint or structure for objects, which have properties and methods

in programming, the User class maybe have a property like  $name,$password

and method which is the behavior of the class like login, register

Class visibility 

Every method and property must declare with visibility

like public $name.

There are 3 types of class visibility 

  1. Public        -method or property  can be access anywhere  
  2. private       -method or property   can be accessed just within the same class
  3. protected  -method or property   can be accessed just within the class and other classes derived from that class

How to make a class

now let's create our User class

class User{
  public $name;
  public $email;
  public $password;

  public function login($email,$password){
    return 'Loggin';


$user = new User();
$user->name = "Qandeel";
echo $user->login('','^@^Yhu');

as you can see this class has three properties name, email, password, and login methods.

to make an object of User class we use the New keywords like $user = new User();