Lesson 12

Introducing Object-Oriented Programming

Programmers strive to write code that has fewer errors, is easier to read, and is easier to maintain, and they strive to write that code faster. Object-oriented programming (OOP) gives you more tools to do that. In this lesson you learn the reasons for using OOP and the basic concepts behind it.

Understanding the Reasons for Using OOP

Object-oriented programming is a way of coding that organizes your programs, encourages consistency, reduces redundancy and complexity, increases flexibility, and promotes better security.

It enables you to create building blocks of basic functionality. You can then reuse these blocks, add on to the blocks, or even override parts of the blocks to create more complex structures. Being able to reuse code in this flexible manner means you have fewer bugs when creating your programs, which makes them more reliable over time.

OOP also uses what is called encapsulation. You use encapsulation every time you use local variables in functions because the variables have local scope and can't be seen outside of the function. Encapsulation is the concept that what you do in one section of your program is not affected by and does not affect another section. OOP has similar structures that encapsulate data and actions. You are inside your house with the shades drawn and you control the door through which you receive and disseminate information.

Additionally, OOP is well suited for implementing both design patterns, ...

Get PHP and MySQL® 24-Hour Trainer now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.