0
0
PhpHow-ToBeginner · 3 min read

How to Create Class in PHP: Syntax and Example

In PHP, you create a class using the class keyword followed by the class name and curly braces containing properties and methods. Use public, private, or protected to set access levels for properties and methods.
📐

Syntax

A PHP class is defined using the class keyword, followed by the class name and curly braces. Inside, you can declare properties (variables) and methods (functions). Access modifiers like public, private, and protected control visibility.

  • class: keyword to start a class
  • ClassName: the name of your class (start with uppercase by convention)
  • Properties: variables inside the class
  • Methods: functions inside the class
  • Access modifiers: control who can use properties/methods
php
class ClassName {
    public $property1;
    private $property2;

    public function method1() {
        // code here
    }

    private function method2() {
        // code here
    }
}
💻

Example

This example shows how to create a simple class Car with a property color and a method displayColor() that prints the car's color.

php
<?php
class Car {
    public $color;

    public function __construct($color) {
        $this->color = $color;
    }

    public function displayColor() {
        echo "The car color is " . $this->color . ".";
    }
}

$myCar = new Car("red");
$myCar->displayColor();
?>
Output
The car color is red.
⚠️

Common Pitfalls

Common mistakes when creating classes in PHP include:

  • Forgetting the $this keyword to access properties inside methods.
  • Not using access modifiers, which can lead to unintended access.
  • Missing the constructor method __construct() to initialize properties.
  • Using incorrect case for class names or methods (PHP is case-insensitive for class names but case-sensitive for methods).
php
<?php
// Wrong: missing $this to access property
class WrongCar {
    public $color;
    public function showColor() {
        echo "Color is " . color; // error: undefined constant 'color'
    }
}

// Right: use $this->property
class RightCar {
    public $color;
    public function showColor() {
        echo "Color is " . $this->color;
    }
}
?>
📊

Quick Reference

Here is a quick summary of PHP class basics:

ConceptDescriptionExample
classDefines a classclass MyClass {}
propertyVariable inside classpublic $name;
methodFunction inside classpublic function greet() {}
constructorSpecial method to initializepublic function __construct() {}
$thisRefers to current object$this->property
access modifiersControl visibilitypublic, private, protected

Key Takeaways

Use the class keyword to define a class with properties and methods.
Access properties inside methods using $this->propertyName.
Use __construct() to initialize object properties when creating an instance.
Always specify access modifiers like public or private for clarity and security.
Remember PHP class names are case-insensitive, but method names are case-sensitive.