Course Content
Autoloading
0/1
Exceptions Handling
0/1
PHP OOP
About Lesson

What are the properties of PHP OOP:

1.Typed properties:

Typed properties, allow you to specify the data type of class properties. This feature enhances code clarity, improves code quality, and helps prevent runtime errors by enforcing stricter data typing within classes.

Declaring Typed Properties:

Typed properties are declared using a colon (:) after the property name, followed by the desired data type:

PHP
<?php

class MyClass {

    public string $name;

    public int $age;

    public ?float $salary; // Nullable typed property

}

?>

Supported Data Types:

Scalar Types: int, float, string, bool.

Compound Types: array, object.

Nullable Types: You can denote a type as nullable by adding a ? before the type (e.g., ?int, ?string, ?float).

Nullable Typed Properties:

You can specify a typed property as nullable by using ? before the data type. Nullable properties can accept either a value of the specified type or null.

PHP
<?php

class MyClass {

    public ?string $name; // Nullable string property

}

?>

Benefits of Typed Properties:

Enhanced Code Clarity: Clearly specifying the data type of properties improves code readability and understanding.

Improved Code Quality: Enforcing data typing helps catch type-related errors at compile-time rather than at runtime.

Better IDE Support: IDEs can provide better code suggestions and type hinting based on typed properties.

Example Usage:

PHP
<?php

class Person {

    public string $name;

    public int $age;

    public function __construct(string $name, int $age) {

        $this->name = $name;

        $this->age = $age;

    }

}

In the example above, the name property is expected to hold a string value, and the age property is expected to hold an integer value. If you attempt to assign a value of the wrong type to a typed property, PHP will generate a fatal error.

Typed properties offer a valuable addition to PHP’s object-oriented features, promoting better code quality, maintainability, and reliability in PHP applications.

2.Readonly properties:

Readonly properties, allow you to define class properties that can only be initialized once, typically within the constructor. Once set, the value of a readonly property cannot be changed throughout the object’s lifetime.

Declaring Readonly Properties:

Readonly properties are declared using the readonly modifier before the property declaration.

PHP
<?php

class MyClass {

    readonly public string $name;

    readonly protected int $age;

}

?>

Initialization of Readonly Properties:

Readonly properties can only be initialized once, usually within the constructor of the class.

After initialization, attempts to modify the value of a readonly property will result in a fatal error.

Benefits of Readonly Properties:

  1. Immutable State: Readonly properties promote immutability, ensuring that the state of an object remains unchanged once initialized.
  2. Enhanced Code Clarity: Clearly defining readonly properties communicates the intention that certain data should not change after object creation.
  3. Prevention of Accidental Modification: Readonly properties help prevent accidental modification of critical data within objects.

Example Usage:

PHP
<?php

class Person {

    readonly public string $name;

    readonly protected int $age;

    public function __construct(string $name, int $age) {

        $this->name = $name;

        $this->age = $age;

    }

}

$person = new Person("John", 30);

echo $person->name; // Output: John

// Attempt to modify a readonly property will result in a fatal error

// $person->name = "Jane";

?>

In the example above, the name and age properties of the Person class are declared as readonly. They can only be initialized once, during object construction. Any attempt to modify these properties after initialization will result in a fatal error.

Readonly properties provide a valuable feature for creating more robust and predictable object-oriented code in PHP, enhancing code clarity and reliability.