0

Inherited an old CakePHP site and I'm trying to figure out what some functions do. I have several functions that have the same name as another function but with an underscore first, e.g. save() and _save(). However the function _save() is never called in any context, though save() is.

I read this question and it looks like it's from an old worst-practices exercise, but that doesn't really explain why it's in my code; you still have to call function _save() as _save() right? If there's no calls to _save() is it safe to remove?

I want it gone, even the save() function wasn't supposed to be there, rewriting perfectly good framework functionality. It looks like an older version of the same function, but there's no comments and I don't know if there's some weird context in which php/Cake will fall back to the underscored function name.

Here's the code for the curious. On closer inspection it appears the underscored functions were old versions of a function left in for some reason. At least one was a "private" method being called (from a public function of the same name, minus the underscore...):

    function __save() {
    $user = $this->redirectWithoutPermission('product.manage','/',true);

    if ($this->data) {
        $this->Prod->data = $this->data;
        $saved_okay = false;
        if ($this->Prod->validates()) {
            if ($this->Prod->save()) $saved_okay = true;
        }
        if ($saved_okay) {
            $product_id = ($this->data['Prod']['id']) ? $this->data['Prod']['id'] : $this->Prod->getLastInsertId();
            if ($this->data['Plant']['id']) {
                $this->data['Prod']['id'] = $product_id;
                $this->Prod->data = $this->data;
                $this->Prod->save_plants();
                $this->redirect('/plant/products/'.$this->data['Plant']['id']);
            } else {
                $this->redirect('/product/view/'.$product_id);
            }
            die();
        } else {
            die('did not save properly');
        }
    } else {
        die('whoops');
    }
}


function save() {
    $user = $this->redirectWithoutPermission('product.manage','/products',true);

    if ($this->data) {
        $this->Prod->data = $this->data;
        if ($this->Prod->validates()) {
            $this->Prod->save();

            $gotoURL = isset($this->data['Navigation']['goto'])?$this->data['Navigation']['goto']:'/';
            $gotoURL = str_replace('%%Prod.id%%', $this->data['Prod']['id'], $gotoURL);

            if (isset($this->data['Navigation']['flash'])) {
                $this->Session->setFlash($this->data['Navigation']['flash']);
            }
            if (isset($this->params['url']['ext']) && $this->params['url']['ext']=='ajax') {
                $value = array(
                    'success'=>true
                    ,'redirect'=>$gotoURL
                );
                print $this->Json->encode($value);
            } else {
                $this->redirect($gotoURL);
            }
        } else {
            $value = array(
                'success'=>false
                ,'message'=>"You have invalid fields."
                ,'reason'=>'invalid_fields'
                ,'fields'=>array(
                    'Prod'=>$this->Prod->invalidFields()
                )
            );
            print $this->Json->encode($value);
        }
    } else {
        $this->redirect('/products');
    }
    die();
}
Community
  • 1
  • 1
Ben Brocka
  • 2,006
  • 4
  • 34
  • 53
  • I'm not sure since I don't play with CakePHP for some years, but underscores are commonly used to differ local functions from inherited ones. – yoda Sep 08 '11 at 15:30
  • But in that case you still have to call the underscored function directly, right? I've confirmed that there is _no_ single call to this function (and others). I really just need to know if in any context a call to `save` would hit this function instead, as that function _is_ called – Ben Brocka Sep 08 '11 at 15:32
  • Doesn't CakePHP have profiling? – yoda Sep 08 '11 at 15:47
  • Not per se, but in testing I haven't been able to get the function to call...I'll have real profiling once I'm on PHP 5.3 but that's not yet – Ben Brocka Sep 08 '11 at 15:51
  • Can you paste the suspect code? – yoda Sep 08 '11 at 15:52
  • 1
    It's possible that `_save()` is called by legacy applications built on top of CakePHP, and is now provided by cake for backwards compatibility. – Frank Farmer Sep 08 '11 at 19:51
  • I added an example of code. It's not provided by cake, it's all code made by our developer. There's a `__save()` method in one of the CakePHP models, but it's for `saveAll()` which we aren't using to my knowledge. – Ben Brocka Sep 08 '11 at 20:13

1 Answers1

1

I had hoped to learn whether or not some convention applied to this situation, but from testing I've found the functions are not called which is really the answer to the question I asked.

Ben Brocka
  • 2,006
  • 4
  • 34
  • 53
  • Functions with a single, leading underscore are just normal functions. ["A valid function name starts with a letter or underscore, followed by any number of letters, numbers, or underscores."](http://www.php.net/manual/en/functions.user-defined.php) One exception are the [magic-methods](http://php.net/manual/en/language.oop5.magic.php), which start with two underscores. – feeela Sep 08 '11 at 19:50