DEV Community

Cover image for OOP Concepts: What's Your Biggest Challenge?
Max Zhuk
Max Zhuk

Posted on

OOP Concepts: What's Your Biggest Challenge?

Hey, fellow developers!πŸ§‘πŸΌβ€πŸ’»

I'm currently working on a book about Object-Oriented Programming in PHP and TypeScript, and I'm curious about your experiences with OOP.

As developers, we all have those concepts that make us scratch our heads from time to time. So, I want to ask: Which OOP concept do you find most challenging to master?

  • Inheritance
  • Polymorphism
  • Encapsulation
  • Abstraction

Drop your answer in the comments, and let's discuss why! Your insights might even help shape some explanations in my upcoming book.

To kick things off, here's a quick PHP 8 snippet showcasing polymorphism:

interface Logger
{
    public function log(string $message): void;
}

class ConsoleLogger implements Logger
{
    public function log(string $message): void
    {
        echo "Console: $message\n";
    }
}

class FileLogger implements Logger
{
    public function log(string $message): void
    {
        file_put_contents('log.txt', "File: $message\n", FILE_APPEND);
    }
}

function doSomething(Logger $logger)
{
    $logger->log("Operation completed");
}

doSomething(new ConsoleLogger());
doSomething(new FileLogger());
Enter fullscreen mode Exit fullscreen mode

This example demonstrates how different classes can implement the same interface, allowing for flexible and extensible code.

What's your take on polymorphism? Love it? Hate it? Share your thoughts!


Photo by rivage on Unsplash

Top comments (2)

Collapse
 
efpage profile image
Eckehard

I just wrote a post about the "S" in the SOLID principle which is about the challenges of inheritance. Maybe you can find some inspiration there.

Collapse
 
mukesh_singhania_1992 profile image
Mukesh Singhania

Working on abstraction fundamentals