简体   繁体   中英

Load Multiple Extensions of a Class with only one Instance

Brief Explanation

I am currently building a web application which includes many extensions of a parent (base class). Much like the following structure:

Class A // Base Class
    -> Class B // Extension
    -> Class C // Extension
    // etc

The extensions hold functions that are specific to that area of the site, so for example, the 'documents' section of my site will have its own class (an extension of the base class), and this class will house all the functions relevant to open, editing, deleting documents etc...

However, the main navigation menu of my application has an interactive dropdown that displays information that is gathered by methods within several of these 'extension' classes.

My Question

Currently, in order to access all the methods needed to gather the data for my navigation menu, I must create an instance of several of the extended classes like so:

require_once(class.a.php);
require_once(class.b.php);
require_once(class.c.php);

// Create an instance of Class B to get the data for the first dropdown
$b = new B();

// Gather the data for the first dropdown
$dropdown[0] = $b->get_document_data();

// Create an instance of Class C to get the data for the second dropdown
$c = new C();

// Gather the data for the second dropdown
$dropdown[1] = $c->get_settings_statistics();

Whilst creating a new instance of an object is not exactly 'the end of the world', my construct function for the parent class is quite large, thus, I do not want to have more than one instance per page...

Resultantly, I can only see one other option where I simply place the necessary methods into the parent class... Given the way my classes work, this is not very practical!

Therefore, what I would like to know is whether or not you can create an instance of an object, and then individually load separate extensions for it?

Alternatively, if you can think of a better way of achieving this, please feel free to provide your view...

You could just favor composition over inheretance:

 class A {
     public function __construct(){ /* heavy lifting here */ }

     public function coolSharedMethod(){}
 }

 class B {
     protected $a;
     public function __construct(A $myA){
          $this->a = $myA;
     }

     // proxy methods through
     public function coolSharedMethod(){
          return $this->a->coolSharedMethod();
     }
 }

If the initialization of A is the same but it contains state that needs to be changed on a case by case basis you might want to look into the prototype design pattern .

 $a = new A();
 $b = new B(clone $a);
 $c = new C(clone $a);

I would probably use generic static methods for each class and pass around an object in an IoC pattern:

<?php

class A
{
    public static function init_nav_menu(&$menu_object)
    {
        // do nothing in base class
    }
}

class B extends A
{
    public static function init_nav_menu(&$menu_object)
    {
        // manipulate menu_object here, giving it info needed for dropdown
    }
}

// (load class files)

$menu = new stdClass;
B::init_nav_menu($menu);
//C::init_nav_menu($menu);
//...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM