Skip to main content

PHP Properties

Properties are variables that belong to an object.

They represent the state of an object.

Examples:

A Player object may contain:

  • Name
  • Rank
  • Health
  • Money
  • Kills
  • Position

A Server object may contain:

  • MOTD
  • Port
  • Max Players
  • Online Players

What is a Property?

Example:

class Player {

public string $name;

}

Property:

$name

belongs to every Player object.


Creating Objects

$player =
new Player();

Setting value:

$player->name =
"Aayan";

Reading value:

echo $player->name;

Output:

Aayan

Visualization

Player Object

├── name = Aayan
├── money = 5000
└── rank = Owner

Multiple Properties

class Player {

public string $name;

public int $money;

public int $kills;

}

Example

$player =
new Player();

$player->name = "Aayan";

$player->money = 5000;

$player->kills = 15;

Typed Properties

PHP 7.4 introduced typed properties.

Example:

public int $money;

This means:

Only integers allowed.

Invalid

$player->money =
"Hello";

Error:

Cannot assign string to property.

Available Types


Integer

public int $money;

String

public string $name;

Float

public float $health;

Boolean

public bool $online;

Array

public array $friends;

Object

public Player $owner;

Mixed

public mixed $data;

Accepts everything.


Nullable Types

public ?Player $target;

Means:

Player OR null

Uninitialized Properties

Bad:

class Player {

public string $name;

}

$player =
new Player();

echo $player->name;

Error:

Typed property must not be accessed before initialization

Solutions


Default Value

public string $name = "";

Constructor

public function __construct(
string $name
) {

$this->name =
$name;

}

Default Values

class Player {

public int $money = 0;

}

Example

$player =
new Player();

echo
$player->money;

Output:

0

Property Visibility

Properties may use:

public
protected
private

Public

Accessible everywhere.

public string $name;

Protected

Only current class and children.

protected int $money;

Private

Only current class.

private string $password;

Real Example

class BankAccount {

private int $money = 0;

}

Nobody can modify money directly.


Why Private is Important

Bad:

$player->money =
999999999;

Good:

$player->addMoney(
100
);

This allows validation.


Readonly Properties

PHP 8.1+

public readonly string $uuid;

Can only be assigned once.


Example:

class Player {

public readonly string $uuid;

public function __construct(
string $uuid
) {

$this->uuid =
$uuid;

}

}

Trying:

$player->uuid =
"abc";

Error.


Property Promotion

PHP 8 introduced Constructor Property Promotion.

Instead of:

private string $name;

public function __construct(
string $name
) {

$this->name =
$name;

}

You can write:

public function __construct(

private string $name

) {

}

Much cleaner.


Real Example

class Database {

public function __construct(

private string $host,
private string $user,
private string $password

) {

}

}

Static Properties

Belong to class, not object.

public static int $count = 0;

Access:

Player::$count;

Example

class Player {

public static int $online = 0;

}

Dynamic Properties

Old PHP allowed:

$player->abc = 5;

PHP 8.2:

Deprecated.

Always declare properties.


Objects as Properties

Example:

class Player {

public Inventory $inventory;

}

Memory:

Player

Inventory Object

Objects can contain other objects.


PocketMine Example

Player internally contains:

Player

├── Inventory
├── NetworkSession
├── Effects
├── Permissions
└── Location

All properties.


Property Initialization Flow

new Player()

Constructor

Properties Initialized

Object Ready

Real Example

class Server {

public string $motd;

public int $maxPlayers;

public bool $running;

}

Common Mistakes


Forgetting Initialization

Bad:

public string $name;

then:

echo $this->name;

Using Public Everywhere

Bad:

public int $money;

Prefer:

private int $money;

Using mixed Too Much

Bad:

public mixed $data;

Use proper typing.


Huge Data Classes

Bad:

Player
500 properties

Split responsibilities.


Best Practices

✅ Use typed properties.

✅ Prefer private.

✅ Initialize values.

✅ Use readonly when possible.

✅ Avoid dynamic properties.

✅ Keep classes small.


Exercises


Exercise 1

Create:

class Car {

public string $brand;

public string $color;

public int $speed;

}

Exercise 2

Create:

class Player {

private int $money;

}

Add methods:

addMoney()
removeMoney()

Exercise 3

Create readonly UUID property.


Mini Project

Create:

Server

Properties:

Name
Version
Port
Players
MaxPlayers

Quiz

What are properties?

Answer

Variables belonging to objects.


Which keyword makes a property immutable?

Answer
readonly

Which visibility is most secure?

Answer

Usually:

private

Which PHP version introduced typed properties?

Answer

PHP 7.4


Real PocketMine Example

class Player {

private Inventory $inventory;

private NetworkSession $networkSession;

private string $username;

private int $gamemode;

}

Everything inside Player is represented using properties.


References

https://www.php.net/manual/en/language.oop5.properties.php


Next Chapter

➡ Methods ➡ Constructors ➡ Access Modifiers