Object Oriented Programming

Having learned procedural programming from a young age, it took me a long time to grasp the ideas of object oriented programming. Once you do grasp them, however, I've found that they come in extremely handy.

In this book, we've talked about models and controllers. Your models and controllers can easily be thought of as objects. Imagine a model for cars. We want to be able to create, read, update, and delete (CRUD) cars from our database. So, we create a cars object and give it the CRUD methods.

class cars {
  
  function create() {
  }
  
  function read() {
  }
  
  function update() {
  }
  
  function delete() {
  }
  
}

Now, in any controller where we want to use the cars model, we just include the php file that contains the model, create an instance of the object, and call it's methods.

  require 'model.php';
  $car = new cars;
  $car->create();

That's it. Three lines of code and we've added a new car to our database using our object oriented model.

comments powered by Disqus
pure_php/object_oriented_programming.txt · Last modified: 2020/06/01 22:53 (external edit)