3

I'm still learning how to use classes and I was wondering when should I use the extends keyword (extend a class).

Let's say I have a class that does thing A (and it's a singleton). Is it ok to extend it to another one that does thing B, even if class B is not really related to class A, but it uses a lot of its methods? Or should I create a new class?

1
  • Could you explain in what circumtances you need to extend singleton class ? Maybe there is other design approach... Did you hear about dependency injection pattern ? Commented Jun 25, 2011 at 3:41

4 Answers 4

5

When it comes to Object-Oriented programming generally a class does a specific task, such as hold user information, or input / output control.

When you have let's say 1 object that is used to control the output, you can set a method within that object to control the output type, or how a each specific content should be handled, but that would mean that the objects sole purpose has been broken, as it's now not just sending content, but it's also manipulating content depending on it's type.

A way in which we use to over come this is by grouping a set of tasks into a group of classes / objects, example:

abstract class Output
{
    public $content_type = 'text/plain';

    public function _send(){}
}

The output class would obviously have much more methods but only methods related to ouputting content, such as headers etc, and not the manipulation of the content, then you would have something like:

class HTMLOutput extends Output
{
     public $content_type = 'text/html';
}

and for the manipulation of the content:

class CSSOutput extends Output
{
    public $content_type = 'text/css';

    public _send()
    {
         if($_SERVER['APP']['Settings']['CSSCompress'] == '1')
         {
             $this->_compress_css();
         }

         parent::_send();
    }

    private function _compress_css()
    {
         $this->content = ; //Some compression lib
    }
}

This is the main architecture points that I abide by when creating and organizing many grouped classes / objects.

Sign up to request clarification or add additional context in comments.

Comments

4

In object-oriented programming, a class should extend another class generally if it's a special type of that class with more specific uses.

Example: class Bike extends Automobile

Bad Example: class Car extends Bike (may share similar methods, but this doesn't really make sense)

Comments

2

Make sure you create a new static members for storing singleton instance and overload the getInstance() method. If not then you will end up accessing A instance even when you call B::getinstance() since there would be only one static member for both class.

Comments

0

Extends should be for example used if you want to access private variables by both classes. If you could show your work, we could tell you what should/shouldn't you do.

4 Comments

it's really a lot of code :) Basically class A is the heart of the website. It gets the articles from the database (the db has its own class), and displays templates to the screen etc. Class B creates content for the sidebar (something like widgets in WordPress)
And do you peronsally think that you DO need to extend class A by class B ?
I'm not sure. But I think that calling class A methods from B like $this->do_stuff() instead of A::getInstance()->do_stuff() looks nicer
In PHP (and most other OO languages) private members are only accessible to the class implementing them. And usually they are private for a reason. It is called encapsulation, go read up on it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.