Class

Class:
When creating a program to use objects, you need to design a composite of data and code called a class. Each new object based on this class is called an instance of that class.

CLASS in PHP
CLASS in PHP

Method:
The data associated with an object is called its properties,the functions it uses are called methods.
Example of method
By using this special variable called $this, which can be used to access the current object’s properties.

<?php
class User
{
public $name,$password;
function get_password()
{
return $this->password;
}
}
?>

get_password uses the $this variable to access the current object and then return the value of that object's password property.

Subclass:
Once you have created a class, you may find that you need another class that is similar to it but not quite the same. The quick and easy thing to do is to define a new class using inheritance. When you do this, your new class has all the properties of the one it has inheritance from. The original class is called the superclass and the new one is the subclass(or derived class).
For example:

<?php
$object=new User;
print_r($object);
class User
{
public $name,$password;
function save_user()
{
echo "Save User code goes here";
}
}
?>

Creating Object:
To create an object with a specified class, use the new keyword, like this:object=new Class. Here are a couple of ways in which we could do this:

$object=new User;
$temp=new User('name','password');
Translate »