Class in php with example
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
- Public -method or property can be access anywhere
- private -method or property can be accessed just within the same class
- 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
<?php
class User{
public $name;
public $email;
public $password;
public function login($email,$password){
return 'Loggin';
}
}
$user = new User();
$user->name = "Qandeel";
echo $user->login('example@example.com','^@^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();