Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Real World Examples of Advanced OOP Features for PHP

Tags:

oop

php

I am trying to improve my knowledge of OOP in PHP and have been researching abstract classes and interfaces.

What I have learned

  • They are both classes that cannot be instantiated themselves but can olny be extended (implemented in the case of interfaces)
  • Abstract classes provide methods and properties for other classes that extend them.
  • If a class uses an abstract method then the class itself must also be abstract.
  • If an abstract method is defined within an abstract class, all child classes must define the details of that method. Methods not defined as abstract can be used in the same way as normal methods.
  • Interfaces define what methods a class that implements it must have. The functionality of the methods are not defined in the interface, the interface just offers a list of methods that must be included in the child class.
  • An interface does not define any properties.
  • Classes can implement as many interfaces as they want to but they must define a method for every one of the interfaces they implement

I think that covers the basics. Please feel free to add to that if you think there's anything I have missed.

What I would like to know is if there are any real world examples of implementation of these classes, especially the interface class. Does anyone know of any open source applications that use them that I can browse to better understand them and see where and when they are used effectively? I have come across book examples which use animals which fails to demonstrate the importance of these classes.

like image 289
Matt Asbury Avatar asked Jan 13 '11 10:01

Matt Asbury


People also ask

What are real life examples of OOPs concepts?

Let's take an example of one of the OOPs concepts with real time examples: If you had a class called “Expensive Cars,” it could contain objects like Mercedes, BMW, Toyota, and so on. The price or speed of these autos could be one of its attributes (data).

What is OOP in PHP with example?

PHP What is OOP? OOP stands for Object-Oriented Programming. Procedural programming is about writing procedures or functions that perform operations on the data, while object-oriented programming is about creating objects that contain both data and functions.

Is written in PHP uses object-oriented programming OOP techniques *?

Object-Oriented Programming (PHP OOP), is a type of programming language principle added to php5, that helps in building complex, reusable web applications.

Which OOPs concept is used in PHP?

Class is one of the most critical OOPs Concepts in PHP. A class can have any number of instances or objects. All the objects of a class will have access to all the data members and member functions of that class.


1 Answers

The final keyword prevents the class being extended by other classes, example:

class Parent
{
}

class Mother extends Parent
{
}

final class Brother extends Mother /* - This class cannot be extended - */
{
}

class Pet extends Brother
{
}

The Pet class will throw an error stating: Fatal error: Class Pet may not inherit from final class (Brother)

This is also available for methods, so if you do not want to allow the methods to be inherited causing the child class to have the same method acting as an override.

http://php.net/manual/en/language.oop5.final.php

Yo used that you would like some real world examples of what interfaces can be used for, well a database abstraction layer

You have 1 base class which provides the basic methods to iterate your database data, but that would use a sub class for the the database type, such as MySql,MsSql etc, each database type would have its own class, but for the base class to make sure that it has these methods they would all implement the same interface.

Example

interface IDatabaseLayer
{
    public function connect();
    public function query();
    public function sanitize();
    //...
}

So the base class knows that MySql and MsSql have the above methods, thus reducing errors and being more organized.

When passing in objects to classes you want to be sure that the Object is of a certain type, PHP5 allows you to define what type of object should be passed into the methods as params.

lets say you have 3 classes

  • DatabaseCredentials
  • DatabaseConnection
  • DatabaseQuery

you can specifically define in the constructuin of DatabaseConnection that you require a DatabaseCredentials class like so:

class DatabaseConnection implements Connectable
{
    public function __construct(DatabaseCredentials $ConnectionDetails)
    {
        $this->Connect($ConnectionDetails->BuildDSN());
    }
}

A good way to really get started is by reading here:

  • http://php.net/manual/en/language.oop5.php

Another feature of PHP5 you may wish to look at is name spaces, this will allow you to keep your code organized, have multiple objects with the same name, makes auto loading more efficiently

Small Example:

namespace Database\MySql
{
    class Database{}
}

namespace Database\MsSql
{
    class Database{}
}

And you can just use like:

use Database;
$Database = new MySql\Database();
like image 90
RobertPitt Avatar answered Oct 10 '22 10:10

RobertPitt