Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Simply (in fact, extremely simply), classes allow you to organize code in logical units as well as provide containers and templates for user-created objects.</p> <p>Let's say you have a car... A car can has a capacity and people inside.</p> <pre><code>class Car { private $people = array(); private $capacity; function __construct($capacity) { $this-&gt;capacity = $capacity; } function addPerson($name) { if(count($this-&gt;people) &gt;= $this-&gt;capacity) { throw new Exception("Car is already at capacity"); } else { $this-&gt;people[] = $name; } } function getPeople() { return $this-&gt;people; } function getCapacity() { return $this-&gt;capacity; } } </code></pre> <p>Now, we can start using those cars:</p> <pre><code>$aliceCar = new Car(2); $aliceCar-&gt;addPerson("Alice"); $bobCar = new Car(4); $bobCar-&gt;addPerson("Bob"); $bobCar-&gt;addPerson("Jake"); </code></pre> <p>I now have 2 cars (instances), which holds different data.</p> <pre><code>echo implode(',', $aliceCar-&gt;getPeople()); // Alice echo $aliceCar-&gt;getCapacity(); // 2 echo implode(',', $bobCar-&gt;getPeople()); // Bob,Jake echo $bobCar-&gt;getCapacity(); // 4 </code></pre> <p>I might also want to have a van, which will have an additional property for doors:</p> <pre><code>class Van extends Car { private $num_doors; function __construct($capacity, $num_doors) { parent::__construct($capacity); // Call the parent constructor $this-&gt;num_doors = $num_doors; } function getNumDoors() { return $this-&gt;num_doors; } } </code></pre> <p>Now let's use that van:</p> <pre><code>$jakeVan = new Van(7, 5); // Van is ALSO a Car $jakeVan-&gt;addPerson("Ron"); //Jake is with Bob now, so his son is driving the Van $jakeVan-&gt;addPerson("Valery") //Ron's girlfriend echo implode(',', $jakeVan-&gt;getPeople()); // Ron,Valery echo $jakeVan-&gt;getCapacity(); // 7 echo $jakeVan-&gt;getNumDoors(); // 5 </code></pre> <p>Now maybe you can see how we could apply those concepts towards the creation of, for example, a <code>DBTable</code> and a <code>User</code> class.</p> <hr> <p>In fact, it's hard to really start explaining why classes simplify one's life without getting into the concepts of Object Oriented Programming (abstraction, encapsulation, inheritance, polymorphism).</p> <p>I recommend you read the following book. It will help you grasp the core concepts of OOP and help you understand why objects to really make your life easier. Without an understanding of those concepts, it's easy to dismiss classes as just another complication.</p> <h1>PHP 5 Objects, Patterns, and Practice</h1> <p><a href="http://rads.stackoverflow.com/amzn/click/1590593804" rel="noreferrer">PHP 5 Objects, Patterns, and Practice http://ecx.images-amazon.com/images/I/51BF7MF03NL._BO2,204,203,200_PIsitb-sticker-arrow-click,TopRight,35,-76_AA240_SH20_OU01_.jpg</a></p> <p>Available at <a href="http://rads.stackoverflow.com/amzn/click/1590593804" rel="noreferrer">Amazon.com</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload