OOP Basics - Difference between extension and instance on a constructor?

Hello all,

I start to see often two approaches that I’m not yet capable of seen the difference.

Let’s say we have two classes.

Class A (super class) and Class B

What is the difference between having something like:


class A 
{
 //stuff
}

class B extends class A
{
//stuff
}

And:


Class A
{
//stuff
}


require ("classA.php");
Class B 
{
  private $_instanceOfA;
  public function __construct() 
  {
    $this->_instanceOfA = new A();
  }
}

I’m asking this because, it seems, at a first sight, that, both ways, allow us to access all classA properties and methods on Class B object instantiation, and I can’t see any other difference on those.

Isn’t this assumption correct?

Thanks,
Márcio

That should be something positive for sure. :slight_smile:

cup: One… oh… one day… We will talk in a way that I could understand all your answers. :wink:

K. Regards
Márcio

I quite simply meant that you have to browse through a good bookshop’s OOP section trying to find the Book which “speaks” to you in a language you both like, enjoy and understand, and be prepared to look at books whose examples are all written in Java or even C#.

It’s a critical decision.

@oikram Reminds me very much of sentiments I held too. Sounds as if you have yet to find the right book. :wink:

If you’re still having problems understanding OOP in PHP, I’d recommend taking a look at Practical PHP Programming (available free online: http://www.tuxradar.com/practicalphp/6/0/0).

While it’s not the world’s best OOP book, it does lay concepts like these out in a way that’s simple to understand and uses concise PHP code to explain the points. I know it helped me when getting started.

I could have sworn someone told me that when you extend a class, it does what you have in option 2 there behind the scenes. Making this question pretty much moot.

Finally!!
After almost a year hearing/reading about “Cars” and “Animals” and “Bicycles”, I can now compare some code to those big basic oop concepts and study them more properly.

Thanks a lot Jake Arkinstall !

You’re comparing Inheritance with Composition.

Basically, they are capable of similar things but have certain benefits.

With Inheritance, the class has certain default methods which you may choose to override if you want to. By default, an empty class extending another shares the same methods and properties, though only has access to protected and public. Inheritance, however, is limited to a single class because a class can only extend one parent class.

With Composition, you can have many objects inside. They don’t need to be of a certain class type - they can be other classes which inherit or extend the base interface/object. This gives you more flexibility. However, the methods obviously aren’t given so you have to implement them yourself. With composition, you’re limited to only accessing public properties and methods.

Personally I prefer composition in a circumstance when both are viable. That’s simply because it offers greater flexibility.

I think It’s mostly suggested to favor composition over inheritance from what I read, and I personally prefer.

Pretend you have a page handler, and that will use a lot of things you want to re-use in other objects it’s a good idea to do it this way:


#
# our parent class
#
class PageHandler{

  public function __construct()
  {
    $this->DB = new Database();
    $this->Session = new Session();
    $this->View = new View();
  } 

  public function Checksomething()
  {
    // Every page load could be required to check something, like a valid session
  }
}

#
# a child class
#
class AboutMe extends PageHandler{

  public function __construct()
  {
    parent::__construct(); // This gives us all the parents goodies like $this->DB, etc
  }

  public function GetPageData()
  {
    return $this->DB->Query("SELECT * FROM AboutMe");
  }

}

#
# a child class
#
class PrivateBio extends PageHandler{

  public function __construct()
  {
    parent::__construct();
  }

  public function GetPageData()
  {
    if ($this->Session->type == 'admin')
    return $this->DB->Query("SELECT * FROM PrivateBio ");
    else
    return 'Only admin can be here';
  }

}

Why would someone have all these different classes? Because you could setup your URL’s to load aboutme.php launches the aboutme class (and the parent also), and privatebio.php launches privatebio. Then you have an easy re-usable base for all your pages that will share a lot of the needed features.

It might be a kind of cheezy example but that’s a little better than bicycles I hope, haha I read a lot about those too.