In PHP, a class serves as the blueprint for creating objects. It defines the properties (data) and methods (functions) that objects of that class will have. Here's a closer look at classes in PHP:
Defining a Class:
- Classes are defined using the
class
keyword followed by the class name. - The class name should be capitalized and follow camelCase convention.
- Within the curly braces
{}
, you define the class's properties and methods.
OOP Case
Let's assume we have a class named Fruit. A Fruit can have properties like name, color, weight, etc. We can define variables like $name, $color, and $weight to hold the values of these properties.
When the individual objects (apple, banana, etc.) are created, they inherit all the properties and behaviors from the class, but each object will have different values for the properties.
PHP - What are Classes and Objects?
Classes and objects are the two main aspects of object-oriented programming.
Look at the following illustration to see the difference between class and objects:
Class: Fruit
Object: Apple , Banana , Mango
Class: Car
Object: Volvo, Audi, Toyota
So, a class is a template for objects, and an object is an instance of a class.
When the individual objects are created, they inherit all the properties and behaviors from the class, but each object will have different values for the properties.
Look at the next chapters to learn more about OOP.
Define a Class
A class is defined by using the class
keyword, followed by the name of the class and a pair of curly braces ({}). All its properties and methods go inside the braces:
Syntax
class Fruit {
// code goes here...
}
?>
class Fruit {
// Properties
public $name;
public $color;
// Methods
function set_name($name) {
$this->name = $name;
}
function get_name() {
return $this->name;
}
}
?>
Define Objects
Classes are nothing without objects! We can create multiple objects from a class. Each object has all the properties and methods defined in the class, but they will have different property values.
Objects of a class are created using the new
keyword.
In the example below, $apple and $banana are instances of the class Fruit:
class Fruit {
// Properties
public $name;
public $color;
// Methods
function set_name($name) {
$this->name = $name;
}
function get_name() {
return $this->name;
}
}
$apple = new Fruit();
$banana = new Fruit();
$apple->set_name('Apple');
$banana->set_name('Banana');
echo $apple->get_name();
echo "<br>";
echo $banana->get_name();
?>
class Fruit {
// Properties
public $name;
public $color;
// Methods
function set_name($name) {
$this->name = $name;
}
function get_name() {
return $this->name;
}
function set_color($color) {
$this->color = $color;
}
function get_color() {
return $this->color;
}
}
$apple = new Fruit();
$apple->set_name('Apple');
$apple->set_color('Red');
echo "Name: " . $apple->get_name();
echo "<br>";
echo "Color: " . $apple->get_color();
?>
PHP - The $this Keyword
The $this keyword refers to the current object, and is only available inside methods.
Look at the following example:
class Fruit {
public $name;
}
$apple = new Fruit();
?>
So, where can we change the value of the $name property? There are two ways:
1. Inside the class (by adding a set_name() method and use $this):
<?php
class Fruit {
public $name;
function set_name($name) {
$this->name = $name;
}
}
$apple = new Fruit();
$apple->set_name("Apple");
echo $apple->name;
?>
Outside the class (by directly changing the property value):
<?php
class Fruit {
public $name;
}
$apple = new Fruit();
$apple->name = "Apple";
echo $apple->name;
?>
PHP - instanceof
You can use the instanceof
keyword to check if an object belongs to a specific class:
<?php
$apple = new Fruit();
var_dump($apple instanceof Fruit);
?>
Key Points:
- Properties: Define the data associated with an object. They can be public (accessible from anywhere), private (accessible only within the class), or protected (accessible within the class and subclasses).
- Methods: Define the behavior of an object. They can perform actions on the object's properties or interact with other objects.
- Constructor and Destructor: Special methods that handle object creation and destruction, respectively.
- Visibility: Use access modifiers like
public
,private
, andprotected
to control access to properties and methods.
Creating Objects:
- Use the
new
keyword followed by the class name to create an object. - You can then access the object's properties and methods using the object variable.
Comments
Post a Comment