Jump to content

Anything other than simple class inheritance escapes me.


KillGorack
Go to solution Solved by maxxd,

Recommended Posts

Hi,

I have a need/desire to have a one to many class inheritance but other than the example below I cannot get it to work. Is this below example something that's used or should I be extending somehow, traits, use. I'm kina lost.

<?php

  class mainone{
    function __construct(){}
    public function buildThing()
    {
      $thing = array();
      $a = new a;
      $thing['a'] = $a->get();
      $b = new b;
      $thing['b'] = $b->get();
      $c = new c;
      $thing['c'] = $c->get();
      $d = new d;
      $thing['d'] = $d->get();
      $e = new e;
      $thing['e'] = $e->get();
      return $thing;
    }
  }

  class a{
    function __construct(){}
    function get(){return "This is the letter a";}
  }

  class b{
    function __construct(){}
    function get(){return "This is the letter b";}
  }

  class c{
    function __construct(){}
    function get(){return "This is the letter c";}
  }

  class d{
    function __construct(){}
    function get(){return "This is the letter d";}
  }

  class e{
    function __construct(){}
    function get(){return "This is the letter e";}
  }

  $foo = new mainone;
  echo "<pre>";
  print_r($foo->buildThing());
  echo "</pre>";

 ?>

The above outputs;

Array
(
    [a] => This is the letter a
    [b] => This is the letter b
    [c] => This is the letter c
    [d] => This is the letter d
    [e] => This is the letter e
)

 

Link to comment
Share on other sites

11 hours ago, requinix said:

Sounds like you've decided on a solution. What was the problem?

Not really an issue, just trying to find best practices. In the past I've extended classes but never was successful with more than a couple together.

I'm putting together an array that kinda acts like a back bone for a dbms. Each one of these nested classes is a part of that array that would change depending on stuff like querystrrings, who's logged in or whatever. Later of course accessing one of the nested classes would be necessary.

Link to comment
Share on other sites

20 minutes ago, maxxd said:

Sounds like you're looking at a facade pattern? Or maybe composite or adapter depending on how you want to use the objects.

Oh, those are freaking nice, I will do some reading.. but the facade looks to be what's needed.

Thanks, I'll come back here to let you know if I used any of it. Frustrating that I didn't come across any of those with el'goog..

Link to comment
Share on other sites

1 hour ago, maxxd said:

Sounds like you're looking at a facade pattern? Or maybe composite or adapter depending on how you want to use the objects.

I may have butchered it, but it works, and I can keep the classes in separate files. the simple class loader I have still works.

<?php

  namespace main;

  class kernel{

    protected $qry;
    protected $ses;
    protected $app;
    protected $fld;
    protected $sql;
    protected $acs;

    private static $instance = NULL;

    static public function getInstance()
    {
      if (self::$instance === NULL)
      self::$instance = new kernel();
      return self::$instance;
    }

    public function __construct(
      qry $qry = null,
      ses $ses = null,
      app $app = null,
      fld $fld = null,
      sql $sql = null,
      acs $acs = null
    ){
      $this->qry = qry::getInstance();
      $this->ses = ses::getInstance();
      $this->app = app::getInstance();
      $this->fld = fld::getInstance();
      $this->sql = sql::getInstance();
      $this->acs = acs::getInstance();
    }

    public function buildKernel()
    {
      $kernel = array();
      $kernel['qry'] = $this->qry->get();
      $kernel['ses'] = $this->ses->get();
      $kernel['app'] = $this->app->get();
      $kernel['fld'] = $this->fld->get();
      $kernel['sql'] = $this->sql->get();
      $kernel['acs'] = $this->acs->get();
      return $kernel;
    }

  }

 ?>

 

Output

Array
(
    [qry] => Query string scrubbers
    [ses] => Check the session variables
    [app] => Settings for current application
    [fld] => Create the field array.
    [sql] => Create the initial sql statement.
    [acs] => Ascertain access level.
)

 

Link to comment
Share on other sites

It looks to me like your kernel class is a quasi registry pattern class.  It certainly has all the hallmarks of a Registry class in that it is a singleton, although typically the registry pattern also will only allow one instance of any of the objects it stores.  Your kernel class is a singleton, but it looks like you replicated static public function getInstance() in every one of the classes you use, rather than inheriting that from a base class.  That might have been the place to use a trait, but you'd be better off with a registry class.

I will also point out that because a registry is a sort of global object container, it is seen as a bit of an anti-pattern, although it was certainly good enough for lots of Zend Framework 1.x applications in an earlier era.   

At this point, Dependency injection, and the use of a Dependency injection Container is the solution that frameworks like Symfony and Laravel use instead.  You could implement one yourself, or use an existing DI Container.  

You might consider reading through the Symfony "Create your own framework" docs which includes discussion of how to utilize the symfony Dependency Injection container(DIC) component.

You could also use PHP-DI.  Both of these components have a lot of commonly desirable functions like autowiring, lazy loading and configuration via files or annotations.  The weakness of a registry pattern for example, is that you have to make all those objects before you may even need all the services.  

With that said, if the most pragmatic solution for you is to use a registry, then I would suggest you more fully implement one.  All your classes should not be hardwired to be instantiated statically -- just implement a $registry->set() and $registry->get().  Then you have something that can support other service objects you might need.  

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.