DEV Community

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

Posted on

6 1 2 1

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

Sentry image

See why 4M developers consider Sentry, β€œnot bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

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

Image of Docusign

πŸ› οΈ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more