Object-oriented programming is one of the most important and one of the most used paradigms that a programmer can use.
This paradigm, in fact, is fundamental to write better code and to find a job.
There are very competitive places (like London for example) where a programmer in most cases is hired only if he knows this paradigm and at least one of the different MVC frameworks available.
In this post,
you will find answers and trivia useful for any programmer who is about to bump into Object-Oriented programming and for experienced coders who want to learn more about this paradigm
Follow the series ...
This blog post is the first part of "The complete guide to Object-Oriented Programming: Go from procedural programming to OOP expert in PHP"
If you did not read the other parts yet
You can check the other blog posts following the links below
Introduction to Object-Oriented Programming,
Inheritance and Interfaces in PHP,
More Interfaces and Polymorphism,
Visibility and Static keyword,
Constructor and Magic methods and
Abstract Classes and Extra bits
What's the difference between procedural programming and the OOP?
The Concept of object-oriented programming solves several procedural programming problems while adding new features that provide a modular structure to the project and make it very easy to maintain. It does it by leveraging object and classes.
A class is like a blueprint. It has attributes and methods that can be used by objects when they are instantiated.
What is an object in PHP?
an object is the actual "thing" that is created when we use the new keyword. It inherits all the attributes and methods from the class indicated. When you code it is very likely to work on it. It can be instantiated, updated and deleted.
What are the properties of a class?
Variables that are present within a class are called properties, Rarely, they will be also called attributes and fields and they are defined by the prefix of visibility that can be public, protected or private. Within classes, you can access those properties using the keyword this-> for non-static classes and the Self:: keyword for static classes.
What are methods of a class?
Methods are simply functions. The methods of a class describe the actions that the class can do.
What are inheritances in OOP?
What relationship in OOP does is to provide the inheritance to related classes in a parent-child model. This is very useful as features of a class can be attributed to another related class..
Why use inheritance in OOP?
Inheritance is an amazing way to save time and avoid to write the same code over and over again. It is also true that it has several limitations and you need to be careful whenever you decide to apply it.
How to relate two classes?
To create a relationship between two classes you need to use the extends keyword. Pay attention because a child class must depend only on a single parent, thus it cannot extend multiple classes.
When to use the final keyword in OOP?
The final keyword is a really easy concept understand. It simply prevents methods or classes to be extended..
What are interfaces in PHP?
If you consider the class as a blueprint, an interface is a blueprint for classes. Think of the interfaces as a pure template. In fact, interfaces defined which method a class must implement, while they do not specify how this method needs to be implemented, giving the choice of how to implement it to the class.
How to implement interfaces to class?
To implement an interface you need to use the implements keyword. Note that all the methods in the interface must be included in the class. Unlike inheritance, you can implement as many interfaces as you'd like.
What is polymorphism in object-oriented programming?
Polymorphism is one of the four pillars of OOP. In a practical example polymorphism is: writing the same code, giving it the same name but allowing it to take a slightly different type of argument or input and then output a slightly different result depending on those parameters.
This word only means gathering data together in a way that the rest of the code is not affected by it.
What scope and visibility means?
Visibility in OOP is the concept by which methods and attributes can be used or hidden in different parts of the code.
What are public variables and methods?
Public methods and attributes can be accessed viewed or updated from everywhere in the code, within a class, from a subclass or even outside the class..
What protected means in OOP?
When declaring properties and methods as protected you are saying that this data is available everywhere inside the class and in any of the classes that extend it.
Where can I use private methods and attributes?
Private is the highest level of protection. Private properties and methods can be accessed only from within the class they have been declared.
How to use the static method in PHP?
By declaring an element of a class (either properties or methods) as static you are saying to the code that the element will be available to all program without the necessity to instantiate the class.
The complete guide to Object-Oriented Programming
The full version of "The complete guide to Object-Oriented Programming" in an all-in-one edition is available in a Kindle format on Amazon anyway soon the following parts will be published.
There are several paradigms that you can use when programs.
But the concept of object programming will always be the most crucial to learn, especially when programming in PHP.
This FAQ page helps you understand the basic by using simple words that are easy to understand.