Object Oriented Programming in PHP -1
Object Oriented Concepts Terms
Object-oriented programming is considered to be more advanced and efficient than the procedural style of programming.
This efficiency stems from the fact that it supports better code organization, provides modularity, and reduces the need to repeat ourselves.
That being said, we may still prefer the procedural style in small and simple projects. However, as our projects grow in complexity, we are better off using the object oriented style.
Object Oriented is an approach to software development that models application around real world objects such as employees, cars, bank accounts, etc.
A class defines the properties and methods of a real world object. An object is an occurrence of a class.
The three basic components of object orientation are:-
- Object oriented analysis – functionality of the system
- Object oriented designing – architecture of the system
- Object oriented programming – implementation of the application
Before we go in detail, lets define important terms related to Object Oriented Programming.
| Term Name | Description |
| Class | |
| Object | |
| Method | |
| Properties | |
1. Class
This is a programmer-defined data type, which includes local functions as well as local data. You can think of a class as a template for making many instances of the same kind (or class) of object.
- Objects & Classes – learn the basic concepts of OOP including objects and classes.
- The $this keyword – help you understand PHP $this keyword and how to use it effectively.
- Access Modifiers: public vs. private – explain to you the access modifiers in PHP and help you understand the differences between the private and public access modifiers.
Object
An individual instance of the data structure defined by a class. You define a class once and then make many objects that belong to it. Objects are also known as instance.
Member function
These are the function defined inside a class and are used to access object data.
2. Constructor and Destructor
- Constructor – explain to you the constructor concept and how to use it to initialize attributes.
- Destructor – learn how to use destructor to clean resources when the object is deleted.
3. Inheritance
When a class is defined by inheriting existing function of a parent class then it is called inheritance. Here child class will inherit all or few member functions and variables of a parent class.
- Inheritance – how to extend a class for code reuse.
- Call the parent constructor – show you how to call the parent constructor from a child class’s constructor.Call the parent constructor – show you how to call the parent constructor from a child class’s constructor.
- Overriding method – guide you on how to override a parent class’s method in the child class.
- Protected Access Modifier – explain the protected access modifier and how to use protected properties and methods effectively.
Parent class
A class that is inherited from by another class. This is also called a base class or super class.
Child Class
A class that inherits from another class. This is also called a subclass or derived class.
4. Abstract classes
- Abstract Class – guide you on abstract classes and how to use them effectively.
5. Interfaces
- Interface – explain to you the interface concept and how to create interfaces.
6. Polymorphism
This is an object oriented concept where same function can be used for different purposes. For example function name will remain same but it take different number of arguments and can do different task.
- Polymorphism – explain the polymorphism concept and show you how to implement polymorphism in PHP using abstract classes or interfaces.
7. Traits
- Traits – introduce you to traits.
8. Static methods & properties
- Static Methods and Properties – show you how to use static methods and properties.
- Class constants – learn how to define class constants using the const keyword.
- Late Static Binding – introduce the late static binding concept and how to use it effectively.
9. Magic Methods
- Magic methods – understand how the magic methods work in PHP.
- __toString() – return the string representation of an object.
- __call() – show you how to use the __call() magic method.
- __callStatic() – show you how to use the __calStatic() magic method.
- __invoke() – learn how to define a function object or function by implementing the __invoke() magic method.
10. Working with Objects
- Serialize Objects– use the serialize() function to serialize an object into a binary string and how to use the __serialize() and __sleep() magic methods
- Unserialize Objects – guide you on how to use the unserialize() function to convert a serialized string into an object. Also, discuss the __wakeup() and __unserialize() magic methods.
- Cloning Objects – show you how to copy an object.
- Comparing Objects – how to compare two objects.
- Anonymous class – learn how to define a class without a declared name.
11. Namespaces
- Namespace – learn how to use namespaces to group the related classes.
12. Autoloading
- Autoloading Class files – learn how to load classes automatically.
- Autoloading using Composer – show you how to use Composer to autoload classes.
13. Exception Handling
- try…catch – show you how to use the try…catch statement to handle exceptions that may occur in your script.
- try…catch…finally – learn how to clean up the resources when an error occurs using the finally block.
- Throw an exception – guide you on how to throw an exception using the throw statement.
- Set an Exception Handler – show you how to use the set_exception_handler function to set a global exception handler to catch the uncaught exceptions.
14. Class / Object Functions
- class_exists – return true if a class exists
- method_exists – return true if an object or a class has a specific method.
- property_exists – return true if an object or a class has a specific property.
Overloading
a type of polymorphism in which some or all of operators have different implementations depending on the types of their arguments. Similarly functions can also be overloaded with different implementation.
Data Abstraction
Any representation of data in which the implementation details are hidden (abstracted).
Encapsulation
refers to a concept where we encapsulate all the data and member functions together to form an object.
Constructor
refers to a special type of function which will be called automatically whenever there is an object formation from a class.
Destructor
refers to a special type of function which will be called automatically whenever an object is deleted or goes out of scope.
In next chapter we will discuss about the class and object