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.
Class versus Object
Section titled “Class versus Object”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; // 100echo $secondWallet->balance; // 40Properties and methods
Section titled “Properties and methods”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.
Object identity
Section titled “Object identity”Two variables may point to the same Object:
$wallet = new Wallet();$alias = $wallet;
$alias->deposit(50);
echo $wallet->balance; // 50Changing 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.
Multiple instances in a backend system
Section titled “Multiple instances in a backend system”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.
Common mistakes
Section titled “Common mistakes”- 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.
Practical example
Section titled “Practical example”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; // 2echo $second->value; // 1Both Objects use the same Method definition, but the Method changes the State of the Object on which it was called.
Summary
Section titled “Summary”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.Knowledge check
Section titled “Knowledge check”- When does a Class definition become an actual Object?
- Why can two Wallet Objects have different balances?
- What happens when two variables point to the same Object?
- Can two different Objects have equal Property values?