Object Oriented Programming PHP


Sharing is caring!

Object Oriented Programming PHP is A Straight Forward Approach to Programming

The programming world, is very interesting and many people are always trying to sharpen their skills in the area of programming, or they are trying to acquire new skills in programming.

In today’s article, we are going to talk about, the object-oriented programming side of PHP.

Object Oriented PHP (OO PHP), can have the ability to frighten, many PHP developers.

One of the reasons why, many PHP developers, don’t really like OO PHP, is due to the concepts of complicated syntax and other roadblocks, which they can face, during the development life cycle of applications.

Developers who use OO PHP, are able to group similar programming tasks, into classes.

When tasks are grouped into classes, then the code is not going to be repeated, so that you “don’t repeat yourself” (DRY), also developers can easily maintain their code, when using OO PHP.

Another aspect of your code which, OO PHP can help you with, is that, when updates to your code are required, you can easily change the code, in one central place, and this change will affect the entire code or parts of the code.

Duplicated data and functionality, can affect the code, when it is time to update the code, because a developer, would have to search the entire directory structure, for data.

A lot of developers are intimidated by OOP, due to the new syntax, and when you just look at it, it does look more complicated.

However, OOP is not as bad as many developers, may have in their minds, because this style of programming is a very simple and straightforward approach to programming.

Alright now let’s define some terms, which are useful to know, in order to understand OO PHP.

In OO PHP you will find something known as a Class: which is used by developers, to group local functions and local data.

If you want to make many instances of the same class or kind of objects, then a class would be a handy tool to use.

Another term, to know in OO PHP is the Object: which is defined by the class as an instance of the data structure.

Instances or objects of the same kind, are defined within one class, and the objects can be a combination of variables, functions, and data structures.

Another important term is the Member Variable: which is a variable, which is defined within a class.

Once an object is created, these variables are called attribute of the object.

Also the data contained in these variables, are only seen within the class and not outside of the class, where the variable is located.

Another important term is the Inheritance: which is a class, which has been defined, with an inherited existing function, of a parent class.

Functions and variables of a parent class, are inherited, by the child class, in the process known as inheritance.

Another important term is Polymorphism: which is, a concept in object-oriented programming, which allows the same function, to be used for different purposes.

Encapsulation is another important term in OO PHP, and it refers to objects, which shows that all data and member functions are encapsulated to form that object.

Constructor is important as well, because this is a special type of function, which, whenever there is an object formation from a class, then the constructor is called.

Last but not least, is the Destructor which is a type of function, which, whenever an object is deleted or goes out of scope, then the destructor is called.

Defining PHP Classes

The general form for defining a new class in PHP is as follows –

&lt;?php class phpClass { var $var1; var $var2 = “constant string”; function myfunc ($arg1, $arg2) { [.] } [.] } ?&gt;</p>