#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2015
    Posts
    3
    Rep Power
    0

    How to implement required method signatures *optionally* used by child class


    I made this thread title as specific as I could but there are these additional constraints related to the question...
    • I create unique/customized classes that you can think of as type "C" that extend a class "B" which in turn extends "A".
    • It does not make sense for class "A" to use the methods which I want to enforce signatures for; it does offer much utility to "B" by "B" extending "A" ("B" must extend "A" due to good OOP design).
    • Class "B" wants the methods to be protected and optional for all customized "C" classes (this rules out PHP Interfaces and also protected abstract functions because the methods are not to be public and are to be optionally used by "C").
    • The methods whose signatures I wish to enforce consistently across all "C"-type classes (each having their own unique properties and thus creating the need for the existence of these classes) needs to be visible only to the class (not public).
    • Each object of the "C" class variety must have its own unique implementation of the methods whose signatures I want enforced (this rules out the use of traits).
    • How many methods am I talking about? just eight; but again, any class of the "C" variety can make use of 0-8 of these methods; it is consistency of their signatures I am concerned about.
    If anyone has an idea how to achieve this: WOW. Because I'm thinking that I need to relax the desire to have the methods be optional and just create empty shell implementations for them in the "C"-type classes wherever they are really not needed, having made the methods abstract functions in class "B". Thankfully class "B" only needs to be extended and so I can accordingly as required make it an abstract class. But is there a better way?
  2. #2
  3. Lazy Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,394
    Rep Power
    9645
    That was a bit too vague for me.

    What kind of code do you want to write that, presumably, does not actually work? I mean like actual code. And how will you use it?
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2015
    Posts
    3
    Rep Power
    0
    Class "A" manages a collection of form fields and their properties; Class "B" does the same as "A" but adds specificity by managing the fields where they are bound to a database table; Class "C" is a specific table in the database. Some tables need to run before/after methods for the select/insert/update/delete database operations but some tables do not. If the methods are used, they need to follow the correct method signature.
  6. #4
  7. Lazy Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,394
    Rep Power
    9645
    Sounds normal. You just need to build all the possible required capabilities into the A and B classes.

    - A has the basic methods for dealing with a collection of form fields
    - B overrides some methods for working with a database, and adds some more for saving and whatever
    - C overrides some methods when it needs to customize an operation, like do something before or after it happens
    PHP Code:
    class // basic form abstraction

        
    protected $fields;

        
    // set $fields
        
    public function __construct(array $fields) { }

        
    // set field values according to $source
        
    public function load(array $source) { }

    }

    abstract class 
    extends // database form abstraction

        
    private $table;

        
    // load fields from $table, call parent constructor
        
    public function __construct($table) { }

        
    // delete
        
    public function delete() { }

        
    // determine if the loaded data is a new record
        
    protected abstract function isNew();

        
    // select field values according to table row
        
    public function loadTable($key) { }

        
    // insert/update entry point
        
    public function save() {
            if (
    $this->isNew()) {
                
    $this->saveAsNew();
            } else {
                
    $this->saveAsExisting();
            }
        }

        
    // update
        
    protected function saveAsExisting() { }

        
    // insert
        
    protected function saveAsNew() { }

    }

    class 
    extends // specific database table

        
    public function __construct() {
            
    parent::__construct("table"); // or set a protected B::$table value and have B load it automatically
        
    }

        public function 
    delete() {
            
    // before code
            
    parent::delete();
            
    // after code
        
    }

        protected function 
    isNew() {
            
    // ...
        
    }

        public function 
    loadTable($key) {
            
    // before code
            
    parent::loadTable($key);
            
    // after code
        
    }

        protected function 
    saveAsExisting() {
            
    // before code
            
    parent::saveAsExisting();
            
    // after code
        
    }

        protected function 
    saveAsNew() {
            
    // before code
            
    parent::saveAsNew();
            
    // after code
        
    }


  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2015
    Posts
    3
    Rep Power
    0
    That's great! Perfect, and thanks for the time to post it. I totally overlooked that way of doing it: override methods of the parent, call the parent's method, and doing needed custom work for the child class both before and after the call to parent::method as needed. Have a nice weekend, Rob.

IMN logo majestic logo threadwatch logo seochat tools logo