PHP 7.1
ZC 1.5.5f
Website
I've been developing an admin tool, for quite some time, that imports datafeeds from a supplier and massages the data to get the most usefulness out of it...follow the website link if you want to know more.
It works well but I do not want to submit it as a contribution until I fully understand when to create custom functions and where to put those functions.
At present, I store my custom functions in admin/includes/functions/extra_functions...the same file that identifies and loads my app in Zencart admin.
The burning question - when is it appropriate to create a custom function?
I'm just getting started with functions and I think I might be over doing it...even after reading hours upon hours of php function training on the PHP website, Zencart functions.php, and other developer websites.
For example, in my file that downloads the feed link, I have multiple queries that perform the identical purpose...to remove all string information before a specified string in a string.
I know, there are a lot of ways to accomplish this already...original code:
PHP Code:
if(!empty(EZ_FEEDS_DEFAULT_FEED)) {
$feed_link = strstr(EZ_FEEDS_DEFAULT_FEED, TEXT_HTTPS);
}
Simple and to the point, right?
Here is what turning it into a function does...
$dirty_string looks like $needle looks like
PHP Code:
function ezfeeds_clean_string($dirty_string, $needle) {
$cleaned_string = strstr($dirty_string, $needle);
return $cleaned_string;
}
$cleaned_string looks like with DSDI Paid All Products - removed.
The function is called like
PHP Code:
$feed_link = ezfeeds_clean_string(EZ_FEEDS_ADD_REMOVE_DATAFEED_LINK, TEXT_HTTPS);
Another example is making a function out a query that is used throughout the entire program to update the configuration table with $value and $key variables.
PHP Code:
function ezfeeds_config_value_key_update($value, $key) {
global $db;
$db->execute("update " . TABLE_CONFIGURATION . " set `configuration_value` = '$value'
where `configuration_key` = '$key' ");
}
Called like
PHP Code:
ezfeeds_config_function_value_key_update($new_link, EZ_FEEDS_ADD_REMOVE_DATAFEED_LINK,'EZ_FEEDS_DEFAULT_FEED');
This could even be taken further to allow any table to be updated...
PHP Code:
function ezfeeds_one_field_update($table, $value, $set_field, $where_field, $key) {
global $db;
$db->execute("update '`' . $table .'`' set '`' . $set_field . '`' = '$value'
where '`' . $where_field . '`' = '$key' ");
}
Is turning a query into a function the accepted practice?
Should I prefix the function name with zen_ or ezfeeds_?
Now, I know these will be useful in many places throughout my whole app but am weary of taking this approach as it seems that I am defeating the readability of my scripts and the purpose of using queries in the first place...just a gut feeling.
I would like feedback from some seasoned developers on which route to take.
My goal is to use as many ZC functions as possible (and pre-built PHP functions) and try not to duplicate any.
Thanks.
Mike
Bookmarks