bionary's avatar

Inheritance & Monitoring Child Methods. Critique Please?

This is not Laravel specific but a question about PHP design patterns.

My Goals:

  • Inherit some basic properties from a parent class.
  • Add more properties in each child class.
  • Pass info to parent class at runtime and have all child classes use that info.
  • Get final log of child class actions.

I've indeed accomplished this in the example below. I've had to mix and match static properties and static methods. Is this normal?

  • Am I using the correct approach?
  • Does this pattern have a name?
  • Are there any problems or improvements to be made with my approach?

Code

Output

array:2 [▼ // routes/web.php:198
  "duration" => 2
  "talks" => array:3 [▼
    0 => "Lisa Simpsons is talking about TV Quotes and says: We are the generation that will inherit the world, Bart."
    1 => "Bart Simpsons is talking about TV Quotes and says: Eat my shorts!"
    2 => "Lisa Simpsons is talking about TV Quotes and says: If anyone wants me, I'll be in my room."
  ]
]

Any input is appreciated, thanks.

0 likes
1 reply
LaryAI's avatar
Level 58

Your approach to using inheritance and static properties/methods in PHP is a valid way to achieve the goals you've outlined. However, there are a few considerations and potential improvements you might want to think about:

Critique and Suggestions

  1. Static Properties and Methods:

    • Using static properties and methods can be useful for shared state across instances, but it can also lead to issues with state management, especially in a multi-threaded environment or when dealing with multiple instances simultaneously. Consider whether the shared state is truly necessary or if instance-specific state would be more appropriate.
  2. Design Pattern:

    • Your approach resembles the Template Method Pattern, where a base class defines the skeleton of an algorithm, and subclasses provide specific implementations. However, the use of static properties for shared state is not typical in this pattern.
  3. Encapsulation:

    • The use of static properties for topic, startTime, and talks breaks encapsulation, as these properties are shared across all instances of the class. If each conversation should be independent, consider moving these properties to instance variables.
  4. Dependency on Static Methods:

    • Static methods like startConversation and endConversation make it difficult to test and manage state. Consider refactoring to use instance methods, which would allow for more flexible and testable code.
  5. Use of Carbon:

    • Ensure that the Carbon class is properly imported and used. If you're using Laravel, now() is a helper function that returns the current time as a Carbon instance.

Improved Code Example

Here's a refactored version of your code that addresses some of these points:

Key Changes

  • Instance-based State: The Conversation class now manages its own state, making it easier to handle multiple conversations independently.
  • Encapsulation: The Simpson class and its children now focus on their specific behavior, while the Conversation class handles the conversation logic.
  • Flexibility: This design is more flexible and testable, as each conversation is an independent instance.

Please or to participate in this conversation.