Reading — step 1 of 7
Learn
PHP has been fully object-oriented since PHP 5 (released 2004). Modern PHP code is heavily class-based. The Modern PHP book and the PHP manual cover this as foundational.
Defining a class
<?php
class Person {
public string $name;
public int $age;
public function __construct(string $name, int $age) {
$this->name = $name;
$this->age = $age;
}
public function greet(): string {
return "Hi, I'm {$this->name} and I'm {$this->age}";
}
}
$ada = new Person('Ada', 36);
echo $ada->greet(); // Hi, I'm Ada and I'm 36
Key syntax:
class Foo { ... }defines a class__constructis the magic constructor — called bynew$thisrefers to the current instance (note the$)->accesses members (NOT.like Java/C#)public string $name(PHP 7.4+) declares a typed property
Visibility
class BankAccount {
private float $balance;
public string $accountNumber;
public function __construct(string $number, float $initial) {
$this->accountNumber = $number;
$this->balance = $initial;
}
public function deposit(float $amount): void {
$this->balance += $amount;
}
public function getBalance(): float {
return $this->balance;
}
}
public— accessible everywhereprivate— only this classprotected— this class and subclasses
Default to private for properties; expose via methods.
Constructor property promotion (PHP 8+)
class Point {
public function __construct(
public readonly float $x,
public readonly float $y,
) {}
}
$p = new Point(3.0, 4.0);
echo $p->x; // 3.0
Declares the property AND assigns from the constructor parameter in one stroke. readonly (PHP 8.1+) makes the property write-once.
Grader note: constructor promotion (PHP 8.0) and
readonly(PHP 8.1) do not work on this course's grader, which runs PHP 7.4. In the exercises, declare typed properties explicitly and assign them in the constructor body — the classic form shown earlier in this lesson.
Note: the lesson on Type Declarations in PHP Intermediate covers this in more depth.
Static members
class Counter {
private static int $count = 0;
public static function increment(): void {
self::$count++;
}
public static function getCount(): int {
return self::$count;
}
}
Counter::increment();
Counter::increment();
echo Counter::getCount(); // 2
staticmakes the member belong to the class, not an instanceself::refers to the current class (used inside the class)::is the static-access operator (NOT->)Class::method()from outside
Class constants
class Status {
const ACTIVE = 'active';
const ARCHIVED = 'archived';
const PENDING = 'pending';
}
echo Status::ACTIVE; // 'active'
Class constants are static and immutable. Use for enum-like values (or use real enums in PHP 8.1+).
__toString — string conversion
class Money {
public function __construct(private int $cents) {}
public function __toString(): string {
return sprintf('$%.2f', $this->cents / 100);
}
}
$m = new Money(12345);
echo $m; // "$123.45" — auto string conversion
echo "$m"; // same — interpolation
__toString is called whenever the object is used in a string context. Must return a string.
Common mistakes
- Forgetting
$this->to access members — barenamelooks for a local variable, not a property. - Using
.instead of->—.is the string concatenation operator in PHP. - Using
=>instead of->—=>is for array entries. - Public properties everywhere — temptation. Encapsulate via private properties + accessor methods.
- Forgetting to declare the constructor
public— without an access modifier, the default is public, but explicit is clearer.
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…