Override MVC methods while keeping arguments
When extending functions in Sugar's MVC framework, it's quite possible you need to call the parent method before or after your code to allow for the original code to run. In the official documentation and in SugarCRM's code itself, the "parent::method()" method is pretty common. However, this can cause a problem with changing function arguments.
For instance, when overriding the save function of a bean, one can use:
<?php
function save($check_notify = FALSE) {
$this->fill_name();
parent::save($check_notify);
}
?>At this moment, when the arguments list or order of the SugarBean method change, this call will prevent the arguments from passing through and you're putting your system at risk. Using the code below, all arguments are properly passed to the parent method:
<?php
function save() {
$args = func_get_args();
$this->fill_name();
call_user_func_array(array('parent', __FUNCTION__), $args);
}
?>Using this code as a template, you'll make sure you're not interfering with future bug fixes or feature updates!
- Topic:
