1.
Classes
A class is a blueprint for creating objects.
class Car {
public $color;
function drive() {
echo "Driving";
}
}
2. Objects
Objects are instances of classes.
$car = new Car();
$car->color = "Red";
$car->drive();
3. Introspection
Check class details at runtime.
echo get_class($car);
print_r(get_class_methods($car));
4. Serialization
Convert object to string.
$str = serialize($car);
$newCar = unserialize($str);
5. Inheritance
Child class inherits parent.
class Vehicle {
function start() { echo "Start"; }
}
class Car extends Vehicle {}
6. Interfaces
Defines required methods.
interface Animal {
public function sound();
}
class Dog implements Animal {
public function sound() { echo "Bark"; }
}
7. Encapsulation
Use private data with methods.
class Test {
private $x;
function setX($val){ $this->x=$val; }
function getX(){ return $this->x; }
}
8. Web Techniques
Example DB usage (basic).
$conn = new mysqli("localhost","root","","db");
9. Constructor
Auto-called on object creation.
function __construct() {
echo "Created";
}
10. Destructor
Called on object destroy.
function __destruct() {
echo "Destroyed";
}
11. Access Modifiers
Control visibility.
class Demo {
public $a;
private $b;
protected $c;
}
12. Static Members
Access without object.
class Test {
public static $num=10;
}
echo Test::$num;
13. Constants
Fixed values.
class Demo {
const PI = 3.14;
}
echo Demo::PI;
14. Abstract Classes
Cannot instantiate directly.
abstract class A {
abstract function show();
}
15. Method Overloading
Using __call().
class Test {
function __call($name,$args){
echo "Method: $name";
}
}
16. Method Overriding
Child overrides parent.
class A {
function show(){ echo "A"; }
}
class B extends A {
function show(){ echo "B"; }
}
17. Polymorphism
Same method different output.
function test($obj){
$obj->sound();
}
18. Magic Methods
Special methods.
function __get($name){
echo $name;
}
19. Namespaces
Avoid conflicts.
namespace A;
class Test {}
20. Exception Handling
Handle errors.
try {
throw new Exception("Error");
} catch(Exception $e){
echo $e->getMessage();
}