Page 1 of 3 123 LastLast
Results 1 to 10 of 24
  1. #1
    Join Date
    Sep 2012
    Location
    West Jefferson, NC
    Posts
    383
    Plugin Contributions
    0

    Default When To Use A Custom Function When Developing Admin App?

    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_FEEDTEXT_HTTPS);

    Simple and to the point, right?

    Here is what turning it into a function does...

    $dirty_string looks like $needle looks like
    https://
    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_LINKTEXT_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_linkEZ_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

  2. #2
    Join Date
    Sep 2009
    Location
    Stuart, FL
    Posts
    12,401
    Plugin Contributions
    87

    Default Re: When To Use A Custom Function When Developing Admin App?

    In my opinion, you're on the right track. If I find a plugin using the same processing multiple times, it's time (tee-hee) to create a function. That way, if changes are needed, the change is made once!

    If you find that many of your functions require the same variables for their processing, it might be time to create a class for the handling. The difference between a class (with functions) and an assortment of functions is that a class also has "state" (i.e. it can remember stuff).

    If you go the many-functions route, I'd suggest prefixing each function name as you indicated in your post (ezfeeds_*) so that you can easily tell that the function is part of your plugin.

  3. #3
    Join Date
    Sep 2012
    Location
    West Jefferson, NC
    Posts
    383
    Plugin Contributions
    0

    Default Re: When To Use A Custom Function When Developing Admin App?

    Quote Originally Posted by lat9 View Post
    In my opinion, you're on the right track. If I find a plugin using the same processing multiple times, it's time (tee-hee) to create a function. That way, if changes are needed, the change is made once!

    If you find that many of your functions require the same variables for their processing, it might be time to create a class for the handling. The difference between a class (with functions) and an assortment of functions is that a class also has "state" (i.e. it can remember stuff).

    If you go the many-functions route, I'd suggest prefixing each function name as you indicated in your post (ezfeeds_*) so that you can easily tell that the function is part of your plugin.
    That way, if changes are needed, the change is made [I][B]once
    I didn't even think about that benefit!

    I've been doing simple php queries for so long that using functions just does not feel right. But, I will forge on with your good advice.

    ezfeeds_ will be the prefix.

    Classes...another day and time.

    Thanks.

  4. #4
    Join Date
    Jul 2012
    Posts
    16,718
    Plugin Contributions
    17

    Default Re: When To Use A Custom Function When Developing Admin App?

    As to where the file(s) of function(s) reside, I have been looking at how/where they are used. For functions that are intended to be used for a single "app" (not necessarily throughout the ZC program) I may be appropriate that the function file(s) be stored in includes/functions and then loaded (include, include_once, require, or require_once) in the applicable application to be made available for operation. By placing in the includes/functions/extra_functions directory the function(s) are loaded for use at all times by all software. Placing the file(s) in this directory is another reason to possibly consider the function prefix to be other than zen_ (until such function is incorporated into the ZC "core"... Yes it can happen.)

    As to the function that queries "concern", take a look through the includes/functions/functions_lookup.php file of the catalog side. Basically, nothing but query lookups or call functions that do query lookups.

    Now considering that these functions are intending to access and manipulate the database, if no one else does, I suggest that the variables being used also go through some level of sanitization before applying directly to the database. For example, in the above ezfields_one_field_update function, if $set_field could be: 'field1` = \'value1\', `field2' and then the variable $value would be set to field2... maybe that's intentional, but it doesn't fit the name of the function and never know what "naughtiness" could be caused. Another thing about the function, it's termed as an update for a single field, is it intended to be a single field of a single record or perhaps a single field for all records? If possibly a field for all records, then I might suggest using some default values for provided parameters such that in their absence an appropriate portion of the query is not included.

    Hmm, just also noticed, how is a null field value anticipated to be handled?

    Good questions by the way. Do also understand, if haven't already, there are several ways to accomplish the same goal, and there are several different opinions in some matters. Keep up the development!
    ZC Installation/Maintenance Support <- Site
    Contribution for contributions welcome...

  5. #5
    Join Date
    Nov 2005
    Location
    los angeles
    Posts
    2,669
    Plugin Contributions
    9

    Default Re: When To Use A Custom Function When Developing Admin App?

    keep up the good work!

    my opinions and my opinions only:

    - i am NOT a fan of single line functions. if it only does 1 thing, do it. why create a function?
    - i HIGHLY recommend using the ZC functions for database access and updates. right now, i believe the in-development version of ZC makes use of the laravel framework. staying with existing functions will make the transition to a newer ZC version much easier - hopefully.
    - with regards to sanitation - ZC should already perform those functions. if every plugin author had to do their own sanitation.... well we are talking much earlier versions. the problem is sanitizing correctly and defining each variable so that you can get the ZC sanitizer to do what you want it to do. ZC already sanitizes _GET and _POST variables through the include of application_top.

    best.
    author of square Webpay.
    mxWorks has premium plugins. donations: venmo or paypal accepted.
    premium consistent excellent support. available for hire.

  6. #6
    Join Date
    Sep 2012
    Location
    West Jefferson, NC
    Posts
    383
    Plugin Contributions
    0

    Default Re: When To Use A Custom Function When Developing Admin App?

    Quote Originally Posted by carlwhat View Post
    keep up the good work!

    my opinions and my opinions only:

    - i am NOT a fan of single line functions. if it only does 1 thing, do it. why create a function?

    - i HIGHLY recommend using the ZC functions for database access and updates. right now, i believe the in-development version of ZC makes use of the laravel framework. staying with existing functions will make the transition to a newer ZC version much easier - hopefully.

    - with regards to sanitation - ZC should already perform those functions. if every plugin author had to do their own sanitation.... well we are talking much earlier versions. the problem is sanitizing correctly and defining each variable so that you can get the ZC sanitizer to do what you want it to do. ZC already sanitizes _GET and _POST variables through the include of application_top.

    best.

    - i am NOT a fan of single line functions. if it only does 1 thing, do it. why create a function?
    That is my biggest concern about turning simple queries into functions...seems like I'm duplicating thing unnecessarily.

    Believe me, I have re-written my program more times than I can remember trying to make it in the format of the most current version of ZC and to use the dB management tools already built in.

    It started out as a pdo version, so I had all unique queries, seperate dB acess, and no functions. It could not take advantage of all the native functions already available in ZC so I converted it to match ZC programming.

    As far as sanitizing goes, it is a pain. I have already modified init_sanitize a couple of times to view external server images and something else I don't recall at the moment. I hated this new change when it occurred but understand the necessity of bringing it in.

    Thanks for your insight.

  7. #7
    Join Date
    Sep 2012
    Location
    West Jefferson, NC
    Posts
    383
    Plugin Contributions
    0

    Default Re: When To Use A Custom Function When Developing Admin App?

    Quote Originally Posted by mc12345678 View Post
    As to where the file(s) of function(s) reside, I have been looking at how/where they are used. For functions that are intended to be used for a single "app" (not necessarily throughout the ZC program) I may be appropriate that the function file(s) be stored in includes/functions and then loaded (include, include_once, require, or require_once) in the applicable application to be made available for operation. By placing in the includes/functions/extra_functions directory the function(s) are loaded for use at all times by all software. Placing the file(s) in this directory is another reason to possibly consider the function prefix to be other than zen_ (until such function is incorporated into the ZC "core"... Yes it can happen.)

    As to the function that queries "concern", take a look through the includes/functions/functions_lookup.php file of the catalog side. Basically, nothing but query lookups or call functions that do query lookups.

    Now considering that these functions are intending to access and manipulate the database, if no one else does, I suggest that the variables being used also go through some level of sanitization before applying directly to the database. For example, in the above ezfields_one_field_update function, if $set_field could be: 'field1` = \'value1\', `field2' and then the variable $value would be set to field2... maybe that's intentional, but it doesn't fit the name of the function and never know what "naughtiness" could be caused. Another thing about the function, it's termed as an update for a single field, is it intended to be a single field of a single record or perhaps a single field for all records? If possibly a field for all records, then I might suggest using some default values for provided parameters such that in their absence an appropriate portion of the query is not included.

    Hmm, just also noticed, how is a null field value anticipated to be handled?

    Good questions by the way. Do also understand, if haven't already, there are several ways to accomplish the same goal, and there are several different opinions in some matters. Keep up the development!
    may be appropriate that the function file(s) be stored in includes/functions and then loaded (include, include_once, require, or require_once) in the applicable application
    Great idea...will do.

    includes/functions/functions_lookup.php
    I will look at this one today...sounds like it may save me tons of coding time!

    So, my gut feeling was right. I jumped into writing custom functions way too soon. I first need to thoroughly understand what already exists.

    The functions in my initial post were to simply post my question and not necessarily for use. So, I will ignore your questions concerning them.

    Thanks.

  8. #8
    Join Date
    Sep 2009
    Location
    Stuart, FL
    Posts
    12,401
    Plugin Contributions
    87

    Default Re: When To Use A Custom Function When Developing Admin App?

    There's also a function reference (https://www.zen-cart.com/docs/phpdoc...nctions.html); I'm not sure if it's been updated for zc1.5.5f, though.

  9. #9
    Join Date
    Jul 2012
    Posts
    16,718
    Plugin Contributions
    17

    Default Re: When To Use A Custom Function When Developing Admin App?

    Quote Originally Posted by mikeel100 View Post
    Great idea...will do.



    I will look at this one today...sounds like it may save me tons of coding time!

    So, my gut feeling was right. I jumped into writing custom functions way too soon. I first need to thoroughly understand what already exists.

    The functions in my initial post were to simply post my question and not necessarily for use. So, I will ignore your questions concerning them.

    Thanks.
    Quote Originally Posted by mikeel100 View Post
    So, my gut feeling was right. I jumped into writing custom functions way too soon. I first need to thoroughly understand what already exists.
    While there have been internal changes to some of these and addition of some, I found the following ZC provided area to be very beneficial when first looking through ZC code from both a development and operational perspective. Yes there are some inaccuracies, but from the perspective of review like you are wanting to do, I think the pros far out weigh the cons.

    https://www.zen-cart.com/docs/phpdoc-1-5-0

    Then can navigate through specific function files or in the upper right corner is an option to see all elements which opens up a huge expanse of information related to what is used where, why, what it is intended to represent, etc... Again has its own quirks and has not really been maintained (if for no other reason than a lot of things are also still changing). From here I would often search for some "general" word like manufacturer or just go through all of the entries beginning with zen_ and see what could be obtained from one or a combination of functions to get the desired result.
    ZC Installation/Maintenance Support <- Site
    Contribution for contributions welcome...

  10. #10
    Join Date
    Sep 2009
    Location
    Stuart, FL
    Posts
    12,401
    Plugin Contributions
    87

    Default Re: When To Use A Custom Function When Developing Admin App?

    Quote Originally Posted by lat9 View Post
    There's also a function reference (https://www.zen-cart.com/docs/phpdoc...nctions.html); I'm not sure if it's been updated for zc1.5.5f, though.
    I barfed that link up, here's the correction: https://www.zen-cart.com/docs/phpdoc...functions.html

 

 
Page 1 of 3 123 LastLast

Similar Threads

  1. Replies: 8
    Last Post: 11 Oct 2012, 12:21 AM
  2. Replies: 3
    Last Post: 11 Apr 2011, 02:08 PM
  3. custom 404 error when changing the admin name
    By ray-the-otter in forum General Questions
    Replies: 6
    Last Post: 26 Oct 2010, 12:14 PM
  4. Replies: 2
    Last Post: 31 Jul 2007, 02:40 AM
  5. Added SSL, Now have Parse Error when trying to use ADMIN
    By minson in forum General Questions
    Replies: 2
    Last Post: 22 Mar 2007, 02:05 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
disjunctive-egg
Zen-Cart, Internet Selling Services, Klamath Falls, OR