1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Extending a class, and a method that holds variables?

Discussion in 'XenForo Development Discussions' started by Dannymh, Feb 9, 2016.

  1. Dannymh

    Dannymh Active Member

    Hi,

    I am working on extending a 3rd party plugin. I am fine with that in most cases, because I can simply add the additional things I need, However in this one there is a controller class that essentially sends information to a model, I want to extend this method, however the method has variables/params in it so when i try to extend it I get an incompatibility error, the original code looks like

    PHP:
    public function updatePicks(array $games, array $picks$userId$poolId$weekId, array $ascores, array $hscores)
        {      
            
    $visitorId XenForo_Visitor::getUserId();

            if (
    $visitorId == $userId)
            {      
                for(
    $i=0$i<count($games); $i++) {
                      
    $g $games[$i];
                      
    $p $picks[$i];
                      
    $hs $hscores[$i];
                      
    $as $ascores[$i];

                      
    $pick $this->_getDb()->fetchRow('
                        SELECT *
                        FROM xf_nflj_pickem_pick
                        WHERE user_id = ?
                            AND game_id = ?
                    '
    , array($userId$g));                 
                    
                    
    $writer XenForo_DataWriter::create('NFLJ_Pickem_DataWriter_Pick');
                    if (
    $pick)
                    {
                        
    $writer->setExistingData($pick['pick_id']);
                    }          
                    
    $writer->set('user_id'$userId);
                    
    $writer->set('pool_id'$poolId);
                    
    $writer->set('week_id'$weekId);
                    
    $writer->set('game_id'$g);
                    
    $writer->set('team_id'$p);
                    
    $writer->set('ascoreary'$as);  
                    
    $writer->set('hscoreary'$hs);                                              
                    
    $writer->save();               
                }          
            }              
        }
    I am trying to extend this and I get to the point of my method being called. If I try to parse the same paramaters i get the following error

    Code:
    Declaration of Silvertails_PickemImporter_Model_BonusFE::updatePicks() should be compatible with NFLJ_Pickem_Model_Picks::updatePicks(array $games, array $picks, $userId, $poolId, $seasonId, $weekId)
    
    [LIST=1]
    [*]XenForo_Application::handlePhpError() in XenForo/Autoloader.php at line 119
    [*]XenForo_Autoloader::autoload() in XenForo/Autoloader.php at line 119
    [*]XenForo_Autoloader->autoload() in XenForo/Application.php at line 1050
    [*]XenForo_Application::autoload() in XenForo/Application.php at line 529
    [*]XenForo_Application::resolveDynamicClass() in XenForo/Model.php at line 189
    [*]XenForo_Model::create() in XenForo/Controller.php at line 101
    [*]XenForo_Controller->getModelFromCache() in NFLJ/Pickem/ControllerPublic/Picks.php at line 248
    [*]NFLJ_Pickem_ControllerPublic_Picks->_getPKMPicksModel() in NFLJ/Pickem/ControllerPublic/Picks.php at line 97
    [*]NFLJ_Pickem_ControllerPublic_Picks->actionEdit() in XenForo/FrontController.php at line 351
    [*]XenForo_FrontController->dispatch() in XenForo/FrontController.php at line 134
    [*]XenForo_FrontController->run() in /home/silvert/public_html/index.php at line 13
    [/LIST]
    
    If I try to extend the method without the params I get the following error

    Code:
    Declaration of Silvertails_PickemImporter_Model_BonusFE::updatePicks() should be compatible with NFLJ_Pickem_Model_Picks::updatePicks(array $games, array $picks, $userId, $poolId, $seasonId, $weekId)
    
    [LIST=1]
    [*]XenForo_Application::handlePhpError() in XenForo/Autoloader.php at line 119
    [*]XenForo_Autoloader::autoload() in XenForo/Autoloader.php at line 119
    [*]XenForo_Autoloader->autoload() in XenForo/Application.php at line 1050
    [*]XenForo_Application::autoload() in XenForo/Application.php at line 529
    [*]XenForo_Application::resolveDynamicClass() in XenForo/Model.php at line 189
    [*]XenForo_Model::create() in XenForo/Controller.php at line 101
    [*]XenForo_Controller->getModelFromCache() in NFLJ/Pickem/ControllerPublic/Picks.php at line 248
    [*]NFLJ_Pickem_ControllerPublic_Picks->_getPKMPicksModel() in NFLJ/Pickem/ControllerPublic/Picks.php at line 97
    [*]NFLJ_Pickem_ControllerPublic_Picks->actionEdit() in XenForo/FrontController.php at line 351
    [*]XenForo_FrontController->dispatch() in XenForo/FrontController.php at line 134
    [*]XenForo_FrontController->run() in /home/silvert/public_html/index.php at line 13
    [/LIST]
    
    Essentially I still want those params passed into my code because I have additional things inside the Game and picks array.

    I tried bringing in the parent::actionUpdate() but because the last action out of that is a redirect it inherits that as its data

    I just cant figure out how I can achieve this.

    Dan
     
  2. wang

    wang Well-Known Member

    How are you trying to extend it? Can you please post the code?
     
  3. Dannymh

    Dannymh Active Member

    I have an event listener for the model
    PHP:
    class Silvertails_PickemImporter_Model_BonusFE extends XFCP_NFLJ_Pickem_Model_Picks
    {
        
    /*function updatePicks(array $games, array $picks, $userId, $poolId, $seasonId, $weekId)
        {
            $parent = parent::updatePicks();
            $visitorId = XenForo_Visitor::getUserId();
            if($visitorId ==2)
            {
                echo "<pre>";
                print_r($parent);
                echo "</pre>";
                exit();
            }
           
            return $parent;
        }
        */
    }

    One of the variants I have tried. I have also tried without the params, with &params and a few other variants. I know I am missing something simple on inheriting them but I just cant get my head around it with this baby fatigued brain of mine
     
  4. Jeremy

    Jeremy XenForo Moderator Staff Member

    Your functions and parameters need to match it exactly:
    PHP:
    public function updatePicks(array $games, array $picks$userId$poolId$weekId, array $ascores, array $hscores)
    You cannot omit the last two arguments.
     
    Bob likes this.
  5. katsulynx

    katsulynx Well-Known Member

    You have to call the parent method with the appropriate parameters, else you receive an error.

    Code:
    parent::updatePicks();
    has to be
    Code:
    parent::updatePicks($games, $picks, $userId, $poolId, $seasonId, $weekId)
    Also from a quick glance, it seems you are using the class proxy wrong.
     
    Dannymh likes this.
  6. Dannymh

    Dannymh Active Member

    Alternatively I can probably use a postSave method to achieve the same with a bit of a workaround of
    the incompatibility is from the first array variable being empty, the last two can be omitted here
     
  7. Chris D

    Chris D XenForo Developer Staff Member

    No. They can't. That's not how PHP works. If you're extending a method it needs to be compatible with the method you're extending, as in, they need to be there.

    This is the correct answer:
    PHP:
    $parent parent::updatePicks($games$picks$userId$poolId$seasonId$weekId$ascores$hscores);
     
  8. Dannymh

    Dannymh Active Member

    Genius. It was that simple, calling the parent with the params. Thank you, you have saved me a crazy amount of tinkering
     
    katsulynx likes this.
  9. Dannymh

    Dannymh Active Member

    Whoops my bad. I posted my edited method from my old system where I did a direct edit to the core.
    The actual core does not have those last two params, they are what I am trying to add in.

    Side question, Can I extend the method to add those additional params?
     
  10. HWS

    HWS Well-Known Member

    Why not ask Bob for custom development to extend his code?
     
  11. Dannymh

    Dannymh Active Member

    Why burden him, when its something unique for me?

    I have it all working now. Just adding some more further modifications now
     
  12. Dannymh

    Dannymh Active Member

    Thanks all for your help, I have it working now.
     

Share This Page