1

i want to show a name.name may be in uppercase or in lowercase this will be depend on which class i will pass.i have to approach one is using class and second is interface which one is better and why ?

  1. Solution using Class

    class User{
    
    }
    
    class Iuser extends User{
        private $name;
        function  __construct($name){
            $this->name = $name;
        }
        function getName(){
            return strtoupper($this->name);
        }
    }
    
    class Wuser extends User{
        private $name;
        function  __construct($name){
            $this->name = $name;
        }
        function getName(){
            return strtolower($this->name);
        }
    }
    
    class Name{
      public $u;
      function  __construct(User $u){
          $this->u = $u;
      }
    
      function getName(){
          $name  = $this->u->getName();
          return "Hi My Name is ".$name; 
      }
    }
    
    $name = "Deval Patel";
    $iu = new Iuser($name);
    $wu = new Wuser($name);
    $user = new Name($wu);
    echo $user->getName();
    
  2. Solution Using Interface

    interface User{
        public function getName();
    }
    
    class Iuser implements User{
        private $name;
        function  __construct($name){
            $this->name = $name;
        }
        function getName(){
            return strtoupper($this->name);
        }
    }
    
    class Wuser implements User{
        private $name;
        function  __construct($name){
            $this->name = $name;
        }
        function getName(){
            return strtolower($this->name);
        }
    }
    
    class Name{
      public $u;
      function  __construct(User $u){
          $this->u = $u;
      }
    
      function getName(){
          $name  = $this->u->getName();
          return "Hi My Name is ".$name; 
      }
    }
    
    $name = "Deval Patel";
    $iu = new Iuser($name);
    $wu = new Wuser($name);
    $user = new Name($iu);
    echo $user->getName();
    
shivani parmar
  • 314
  • 2
  • 17

2 Answers2

2

Using the interface solution the getName() method is defined but not implemented in User interface (not shown in your code), so each class implementing it has to define the behaviour to get the name, whilst using abstract classes you may define the standard way to get the name, and override or overload the method in child classes when necessary.

So for your code, I think the best solution is the abstraction.

Remember to use interfaces when you want to force the developer to code the getName() method, and for abstract classes just to allow the developer to use the parent method getName(), or override/overload if necessary. Interfaces gives you more control and code reutilization.

Review the PHP object interfaces doc and PHP class abstraction doc, it may shed some light on your doubt.

Nacho M.
  • 672
  • 4
  • 9
1

I would use classes because you can reuse some code.

abstract class User
{
    protected
        $name;

    public function __construct($name)
    {
        $this->name = $name;
    }

    abstract function getName();
}

class Iuser extends User
{
    public function getName()
    {
        return strtoupper($this->name);
    }
}

class Wuser extends User
{
    function getName(){
        return strtolower($this->name);
    }
}

This way you can still use polymorphism and reuse some common code.

It's better then interface because the code reuse. You can still use an interface for the abstract class User but I think it would be an overkill.

Vasil Rashkov
  • 1,818
  • 15
  • 27
  • Sorry when i try our code even it's not work,without calling parent construct how `$name` variable retrieve @vasil – shivani parmar May 27 '16 at 09:49
  • 1
    @shivaniparmar it's called in the abstract class. When you are extending a class you receive all of its public/protected methods/parameters. So when you do `$userA = new Wuser('Foo');` The __construct of the abstract class User is called. There is no way that it is not working, I tested it before posting. – Vasil Rashkov May 27 '16 at 10:25