Learn, practice, and apply

Object oriented programming came late to PHP. It has been around in other languages like C++, Ruby, Python and JavaScript for much longer, but its demand in PHP is becoming stronger. With that demand for the programming style there is an increasing demand for developers who can program in object oriented PHP. And those programmers typically earn considerably more than their fellow web developers whose background is only limited to procedural PHP.

But this does not tell the full story. Consider the following facts :

  • Procedural programming is inefficient
  • PHP frameworks rely on object oriented PHP
  • Drupal and other CMS systems are now object oriented
  • There are more and more parts of WordPress becoming object oriented

Let's expand on the first fact as it is the most important one - everything else flows from it, i.e. object oriented programming is gaining popularity because it is more efficient. There are great applications written in the procedural style of PHP, but even the best end up in spaghetti code where functions and variables are mixed together. Add in HTML, CSS and JavaScript to this mix and the code becomes something that is, well, inefficient. Object oriented code is more organized. Distinct units are created for a single purpose. This means it is easier to find things in the code (ending those days of spending hours trawling through lines of spaghetti code to find a single element). It is also easier to add, replace or remove parts of code as it only has to be done once. With procedural programming, on the other hand, it is necessary to change every instance. Finally, object oriented programming gives the opportunity to developers to integrate code libraries from other resources. This is done without worrying about name collisions, even though some of the functions in the code might have the same name.

So, back to the original question, why bother learning object oriented PHP?

The answer is:

  • It is increasingly common
  • Developers with this skill earn more
  • The code they write is better

Programming in object oriented PHP requires a different way of thinking. That is why it is important to get an understanding of the fundamentals.

What does the book cover?

This book covers the fundamentals of programming in object oriented PHP. This starts with the basics of writing in an object oriented style. That style applies to everything including classes, objects, methods and properties. It also covers how code in a class can be approached from within the class and, crucially, from outside it.

For example:

  • Creating classes
  • Adding properties to a class
  • Creating objects from a class
  • Setting an object's properties
  • Adding methods to a class

The book also shows developers how to write streaming code that is easier to digest and understand because it more closely resembles human language. Plus the book covers a number of other skills, hints, tips and tricks to make object oriented programming more efficient. This includes:

  • Protecting code inside classes from the outside
  • Object inheritance and how it reduces repetition, therefore saving time
  • Abstracting classes and methods so that code is organized better
  • Other code organizing tips
  • Constructing applications using interfaces and polymorphism
  • Integrating code from other sources and libraries with the help of namespaces

The book also covers the intimidating concept of dependency injection in a practical and easy-to-understand way. Once finished with the dependency injection chapter, object oriented programming becomes even faster.

Who is this book for?

Obviously, this book is for procedural PHP developers who want to improve their skills and take their programming development to a new level. It is also for programmers who have a very basic understanding of object oriented PHP, but who want to learn it properly and get more practice.

Others who will benefit are those programmers who have a background in other objected oriented languages like C++ and Java.

So, in summary, the book is for existing programmers who want to learn object oriented PHP as a new skill.

Getting the most from this book

As already established, this book is designed for existing programmers who want to expand their skill set with object oriented PHP. As a result it is written in a learn-then-practice form.

Every principle, idea or process is described in a simple and concise way. It is easy to read as there is no superfluous text. The flow is also structured so that the reader develops hooks to hang additional knowledge on as their understanding grows and develops.

The material is accompanied by relevant and elucidating code examples for improved understanding.

Once the foundations are laid, the learner gets a plethora of opportunities to practice and bring the material to life. In the practice opportunities, the learner is guided through the process of writing code examples that are based on what he has just learned. This is where proper understanding develops as the learner repeatedly gets a chance to practice what he has just learned.

It is not a passive learning tool. This book is designed to give developers knowledge and skills in object oriented PHP quickly. That is achieved through active learning where programmers think for themselves while reading the explanations and examples and practicing the material.

So, to get the most from this book, approach it with an open mind and be ready to get involved. By doing this you will get a good understanding of object oriented PHP with sufficient knowledge and confidence to apply it practically in your work.

Joseph Benharosh

I'm a senior web developer, an instructor and, the founder of PHPenthusiast.com, a learner-centered online tutorials website that teaches Object Oriented PHP on which this eBook is based. You can find out more at PHPenthusiast.com.

  • Introduction

    • Why bother learning object oriented PHP?

    • The purpose of this book

    • What does the book cover?

    • Who is this book for?

    • Getting the most from this book

  • How to create classes and objects?

    • How to create classes?

    • How to add properties to a class?

    • How to create objects from a class?

    • Objects, what are they good for?

    • How to get an object’s properties?

    • How to set an object property?

    • How to add methods to a class?

    • Conclusion

    • Let’s practice what we have just learned

  • The $this keyword

    • The $this keyword

    • Conclusion

    • Let’s practice what we have just learned

  • Chaining methods and properties

    • Conclusion

    • Let’s practice what we have just learned

  • Access modifiers: public vs. private

    • The public access modifier

    • The private access modifier

    • How to access a private property?

    • Why do we need access modifiers?

    • Conclusion

    • Let’s practice what we have just learned

  • Magic methods and constants unveiled

    • The __construct() magic method

    • How to write a constructor method without risking an error?

    • Magic constants

    • Conclusion

    • Let’s practice what we have just learned

  • Inheritance in object oriented programming

    • How can a class inherit the code of another class?

    • How can a child class have its own methods and properties?

    • The protected access control modifier

    • How to override the parent’s properties and methods in the child class?

    • How to prevent the child class from overriding the parent’s methods?

    • Conclusion

    • Let’s practice what we have just learned

  • Abstract classes and methods

    • How to declare classes and methods as abstract?

    • Can we have non abstract methods inside an abstract class?

    • How to create child classes from an abstract class?

    • Conclusion

    • Let’s practice what we have just learned

  • Interfaces - the next level of abstraction

    • How to declare and implement an interface?

    • Can we implement more than one interface in the same class?

    • What are the differences between abstract classes and interfaces?

    • Conclusion

    • Let’s practice what we have just learned

  • Polymorphism in PHP

    • How to implement the polymorphism principle?

    • Conclusion

    • Let’s practice what we have just learned

  • Type hinting

    • How to do array type hinting?

    • How to do object type hinting?

    • Does PHP support type hinting to basic data types?

    • Conclusion

    • Let’s practice what we have just learned

  • Type hinting for interfaces

    • Why type hinting for objects may not be sufficient?

    • Type hinting for interfaces

    • Conclusion

    • Let’s practice what we’ve just learned

  • Static methods and properties

    • How to define methods and properties as static?

    • How to approach static methods and properties?

    • How to approach the static methods from within the class?

    • When to use static properties and methods?

    • Why static should be used with caution?

    • Conclusion

    • Let’s practice what we have just learned

  • Traits and code inclusion

    • How do traits work?

    • Is it possible for a class to use more than one trait?

    • How is a trait different from inheritance?

    • What are the advantages of using traits?

    • What are the disadvantages of using traits?

    • In which scenarios is it preferable to use traits?

    • Conclusion

    • Let’s practice what we have just learned

  • Namespaces and code integration

    • The directory structure

    • How to define a namespace?

    • How to use a class that belongs to a namespace?

    • How to alias a class from a namespace with a friendly name?

    • How to call a class from the global namespace?

    • Can more than one namespace be imported into the same file?

    • Conclusion

    • Let’s Practice what we have just learned

    • Appendix: Group use declarations in PHP7

  • Dependency injection

    • The problem: tight coupling between classes

    • The solution: dependency injection

    • Why is it a good idea to type hint the injected objects?

    • Conclusion

    • Let’s practice what we have just learned

  • What are exceptions and how to handle them?

    • How to throw an exception?

    • How to catch an exception?

    • Are there any other methods that can help us handle exceptions?

    • What about writing the exceptions to a log file?

    • When to use exception handling?

    • Conclusion

    • Let’s practice what we’ve just learned:

  • PDO - PHP database extension

    • How to connect with the MySQL database through PDO?

    • How to use PDO to insert data into the database?

    • How to use PDO to read from the database?

    • How to use PDO to update the database?

    • How to delete records?

    • How to close the database connection?

    • Conclusion

    • Let’s practice what we have just learned

    • supplementary section

  • How to use Packagist and Composer to integrate existing code libraries into your PHP apps?

    • How to install composer?

    • Installing the package

    • How to add packages?

    • How to update the packages?

    • How to remove a package from Composer?

    • Conclusion

  • How to autoload PHP classes the Composer way?

    • A short reminder about autoloading Packagist code libraries

    • How to directly autoload classes with Composer?

    • How to autoload the PSR-4 way?

    • How to autoload if the directory structure is complex?

    • Conclusion

  • MVC and code organization

    • The model

    • The broader context - MVC

    • The data flow in the MVC pattern

    • The simplest MVC example - retrieving the data from the data source

    • The simplest MVC example (part 2) - getting data from the user

    • Conclusion

  • Epilogue

    • What have we learned?

    • Some additional resources to advance your knowledge