Skip to content

Class and Object

A Class defines the properties and methods available to a kind of object. An Object is a concrete instance created from that definition at runtime.

final class Wallet
{
public float $balance = 0;
public function deposit(float $amount): void
{
$this->balance += $amount;
}
}

Wallet is the Class. It describes a balance Property and a deposit Method, but no Wallet Object exists until code uses new:

$firstWallet = new Wallet();
$secondWallet = new Wallet();

The two Objects were created from the same Class, but each has its own State:

$firstWallet->deposit(100);
$secondWallet->deposit(40);
echo $firstWallet->balance; // 100
echo $secondWallet->balance; // 40

A Property holds part of an Object’s current State. A Method represents behavior available through that Object.

public float $balance = 0;
public function deposit(float $amount): void
{
$this->balance += $amount;
}

Inside an instance Method, $this refers to the current Object. Its full object-context rules are covered in a later lesson.

Two variables may point to the same Object:

$wallet = new Wallet();
$alias = $wallet;
$alias->deposit(50);
echo $wallet->balance; // 50

Changing the Object through $alias is visible through $wallet because there is only one Object with one identity.

By contrast, two calls to new Wallet() create two different Objects even if their Property values happen to be equal.

A backend application may load many Orders, Users, or Wallets at once. Each Object follows the same Class definition while representing one independent instance:

$customerWallet = new Wallet();
$merchantWallet = new Wallet();
$customerWallet->deposit(200);
$merchantWallet->deposit(500);

Keeping their identities and State separate is essential: changing the customer wallet must not accidentally change the merchant wallet.

  • Calling the Class itself an Object before an instance is created.
  • Assuming Objects from the same Class share their instance Properties.
  • Assuming equal Property values mean two variables reference the same Object.
  • Treating an Object as only a bag of data and ignoring its Methods.
  • Leaving Properties untyped when PHP can express the intended Type.
final class Counter
{
public int $value = 0;
public function increment(): void
{
$this->value += 1;
}
}
$first = new Counter();
$second = new Counter();
$first->increment();
$first->increment();
$second->increment();
echo $first->value; // 2
echo $second->value; // 1

Both Objects use the same Method definition, but the Method changes the State of the Object on which it was called.

Class = definition of available Properties and Methods.
Object = runtime instance created from that Class.
State = current Property values of one Object.
Identity = the specific Object, independent of whether another Object has equal values.
  1. When does a Class definition become an actual Object?
  2. Why can two Wallet Objects have different balances?
  3. What happens when two variables point to the same Object?
  4. Can two different Objects have equal Property values?