OOP with PHP

Tram Ho

OOP – The concept of object oriented programming

What is object oriented? OOP – OOP not only uses new coding syntax, it changes the way you think about a problem posed. Many programmers make the mistake of applying inappropriately the object-oriented programming theory.

Relatively, you need to identify verbs and nouns that are necessary for the application. Based on the comparison of these two sets, we can choose an appropriate application design model based on object-oriented or procedural.

Some basic concepts of OOP classes and objects The two most important concepts in OOP are class and object. A class is the definition of the general event. An object object is a concrete implementation of what is defined by the class. To program according to OOP, you must design classes and implement (apply) them to generate the necessary objects.

Moduleization of applications One of OOP’s philosophies is modularization: dividing an application into several separate parts. For example a website has a lot of things to do: interact with the database, send emails, receive data sent from the form, generate HTML for the page … Then each of these, can be developed separately to call them. are modules and sometimes they will correspond to the class. By separating the relationships between such elements, it is much simpler to develop, update, and debug.

Abstract abstraction OOP’s abstraction involves a very general definition of classes – all-encompassing, which is difficult to understand for beginners. For example, instead of directly designing a class that interacts with MySQL database, you design a class that interacts with not a specific database, but general, that class is called an abstraction. From that generic class, when you need to use it specifically for MySQL you will overload the functions, you can define some more specific features that can be used with MySQL, similar to other specific types of databases.

Encapsulation Encapsulation is also a fundamental concept of OOP, which means that the way it behaves so that the request / way of changing the object’s state will not be known by the user. This means it is a black box, no matter how the inside works, only the response of that black box to requests.

Above are very basic concepts, they are clarified through specific examples.

Create class To create a class you use the keyword class followed by the class name, the entire content of the class defined in the pair {}

After having such a class definition, creating an object of the class uses the new operator: $ obj = new MyClass ;, to view the content of the class, var_dump can be used.

You write the following code in the example file test.php and run it

// Out: object (MyClass) Properties and methods are members of the class Inside the class there are variables and functions called with the names of the properties of the class, the methods of the class. The entire set of properties and methods of a class are called members of the class.

The methods are defined, declared inside the class like a normal external function, it can return a value, it can have parameters, default parameters … functionName function meaning in the class.

Note, with the function in the class (method) when defined, it usually starts with the words defining the access scope (visibility you will learn more in the inheritance) with the keywords: public, private or protected if the function definition does not specify one of these keywords, the method is interpreted as public

A class member (methods and properties), accessible when defined as follows:

This public member is accessible anywhere private is only accessible from the definition class itself, so the protected member is only accessible from the definition class itself to the member and the classes that extend it. class (variable) is the same as the regular variable definition but prefixed with keywords that define the reach. The following example defines a class with properties and methods using the access scope definition keywords:

Accessing members with -> and $ this property Once you have an object created from the class (for example $ obj), to access the public member of the class will use the arrow symbol ->. For example $ obj-> name; $ obj-> setAge () …

From a function member of the class, you want to access another function member, in OOP with PHP use the $ this notation as an object, it combines with -> to refer to other members. You write the following code in the example file test.php and run it, notice how to use ->; $ this; protect …

Constructor A constructor is a special type of function in the class, its difference from normal functions is in 3 points:

Inheritance When defining classes (subclasses) you can let that class inherit its properties from another class (called the parent class) with the keyword extends.

Example: create parent class Pet and subclass Dog and Cat inherit from Pet

<? class Pet {public $ name; function __construct ($ pet_name) {$ this-> name = $ pet_name; } function eat () {echo ”

$ this-> name is eating.

“; $ this-> play ();} function sleep () {echo”

$ this-> name is sleeping.

“;} function play () {echo”

$ this-> name is playing.

“;}} class Cat extends Pet {function play () {echo”

$ this-> name is climbing.

“;}} class Dog extends Pet {function play () {echo”

$ this-> name is fetching.

“;}} $ dog = new Dog (‘Satchel’); $ cat = new Cat (‘Bucky’); $ pet = new Pet (‘Rob’); $ dog-> eat (); $ cat-> eat (); $ pet-> eat (); $ dog-> sleep (); $ cat-> sleep (); $ pet-> sleep (); $ dog-> play (); $ cat-> play () ; $ pet-> play ();?>

undefined Interface – Interface Interface it provides names of common functions to implement the code. It can be said that interface is a set of paradigm for implementing code.

To create an interface, the way to create the interface is similar to a class with the keyword being the interface and the body of the methods is not defined. All methods in the interface are public

Example of creating an interface

So you see interface only has interface name and method names. Now a class that implements this interface code will define the class using the implements keyword

When the class implements any interface, all functions of that interface must define the complete code. ILogger interface implementation example

Abstract class The abstract method (function) is a function declared with the abstract keyword, it is just the method name does not contain implementation code. The function is declared as follows:

When a class has at least one abstract function, that class is called an abstract class and in the class declaration also specifies the keyword abstract.

<? php abstract class abstractClass {abstract public function abstractMethod (); // .. methods – other properties} Abstract class in PHP is used as a template class, not used directly, but must implement the abstract class inheritance and redefine abstract functions. Example of an abstract class: <? Php abstract class Person {protected $ firstName; protected $ lastName; public function __construct ($ firstName, $ lastName) {$ this-> firstName = $ firstName; $ this-> lastName = $ lastName; } public function __toString () {return sprintf (“% s,% s”, $ this-> lastName, $ this-> firstName); } abstract public function getSalary (); }?>

With the abstract class Person above, if you use the direct initialization of the class as follows, it will error:

For correct use, it is necessary to inherit the abstract class and redefine the necessary abstract function:

<? php class Employee extends Person {private $ salary; public function __construct ($ firstName, $ lastName, $ salary) {parent :: __ construct ($ firstName, $ lastName); $ this-> salary = $ salary; } public function getSalary () {return $ salary; }} $ e = new Employee (‘John’, ‘Doe’, 5000); echo $ e;

Share the news now

Source : Viblo