PHP Tutorial
PHP Flow Control
PHP Functions
PHP String
PHP Array
PHP Date Time
PHP Object Oriented
Regular Expression
PHP Cookie & Session
PHP Error & Exception handling
MySQL in PHP
PHP File Directory
PHP Image Processing
In PHP, a class is a blueprint for creating objects (a particular data structure), providing initial values for state (member variables, known as properties), and implementations of behavior (member functions or methods).
Here's a tutorial on how to define a class in PHP:
Step 1: Define a Class
First, you use the class
keyword to define a class:
class MyClass { // properties and methods }
Step 2: Add Properties
Inside the class, you can define properties (variables associated with the class):
class MyClass { public $prop1 = "I'm a class property!"; }
In this case, $prop1
is a property that is available to the public, meaning it can be accessed both from within the class and from outside the class.
Step 3: Add Methods
You can also define methods (functions associated with the class):
class MyClass { public $prop1 = "I'm a class property!"; public function setProperty($newval) { $this->prop1 = $newval; } public function getProperty() { return $this->prop1; } }
In this case, setProperty()
and getProperty()
are methods that can be used to set and get the value of $prop1
. The $this
keyword is used to refer to the current instance of the class.
Step 4: Instantiate the Class
Once the class is defined, you can create new instances of the class using the new
keyword:
$obj = new MyClass;
Step 5: Use the Object
Now you can use the properties and methods of the class:
echo $obj->getProperty(); // Outputs: I'm a class property! $obj->setProperty("I'm a new property value!"); echo $obj->getProperty(); // Outputs: I'm a new property value!
In this example, the getProperty()
method is used to echo the value of $prop1
, and the setProperty()
method is used to change the value of $prop1
.
Defining classes is the fundamental concept of Object-Oriented Programming (OOP) in PHP. Classes allow you to organize your code in a way that is easy to manage and understand.
How to Define a Class in PHP:
class MyClass { // Class definition goes here }
Creating a Simple PHP Class:
class Car { public $brand; public $model; public function startEngine() { echo 'Engine started!'; } } // Usage $myCar = new Car(); $myCar->brand = 'Toyota'; $myCar->model = 'Camry'; $myCar->startEngine();
Class Properties and Methods in PHP:
class Person { public $name; private $age; public function __construct($name, $age) { $this->name = $name; $this->age = $age; } public function greet() { echo "Hello, my name is {$this->name}!"; } } // Usage $john = new Person('John', 25); $john->greet();
PHP Constructor in Class Definition:
class Dog { public $name; public function __construct($name) { $this->name = $name; } public function bark() { echo 'Woof!'; } } // Usage $myDog = new Dog('Buddy'); $myDog->bark();
Defining Constants in a PHP Class:
class MathOperations { const PI = 3.14; public function calculateCircleArea($radius) { return self::PI * $radius * $radius; } } // Usage $calculator = new MathOperations(); echo $calculator->calculateCircleArea(5); // Outputs: 78.5
Inheritance in PHP Classes:
class Animal { public $species; public function eat() { echo 'Animal is eating.'; } } class Dog extends Animal { public function bark() { echo 'Woof!'; } } // Usage $myDog = new Dog(); $myDog->species = 'Canine'; $myDog->eat(); $myDog->bark();
Abstract Classes in PHP:
abstract class Shape { abstract public function calculateArea(); } class Circle extends Shape { public $radius; public function __construct($radius) { $this->radius = $radius; } public function calculateArea() { return 3.14 * $this->radius * $this->radius; } } // Usage $circle = new Circle(5); echo $circle->calculateArea(); // Outputs: 78.5
PHP Class Visibility Modifiers (public, private, protected):
class BankAccount { public $balance; // Public, accessible from outside the class private $pin; // Private, only accessible within the class protected $accountNo; // Protected, accessible within the class and its subclasses }
Static Methods and Properties in PHP Class Definition:
class MathOperations { public static function square($number) { return $number * $number; } public static $pi = 3.14; } // Usage $result = MathOperations::square(4); echo $result; // Outputs: 16 echo MathOperations::$pi; // Outputs: 3.14
PHP Traits and Class Composition:
trait Logger { public function log($message) { echo "Log: $message"; } } class User { use Logger; // Other class members... } // Usage $user = new User(); $user->log('User created.');
Using Namespaces When Defining PHP Classes:
namespace MyNamespace; class MyClass { // Class definition... }
PHP Magic Methods in Class Definition:
class MagicExample { public function __construct() { echo 'Constructor called.'; } public function __toString() { return 'This is a magic method example.'; } } // Usage $obj = new MagicExample(); // Outputs: Constructor called. echo $obj; // Outputs: This is a magic method example.