ArtcoInc Posted July 19, 2018 Share Posted July 19, 2018 I am, at best, a "copy and paste" coder. I can get things to work most of the time, but don't always know how, or why. I've just made a content module, copying an existing one and modifying it, pasting in bits from others. It's working, but I'd like some guidance if I should change anything. Here's the code snippet: class cm_footer_conditions_modal { var $code = 'cm_footer_conditions_modal'; var $group = 'footer_scripts'; var $title; var $description; var $sort_order; var $enabled = false; function cm_footer_conditions_modal() { $this->code = get_class($this); $this->group = basename(dirname(__FILE__)); $this->title = MODULE_CONTENT_FOOTER_CONDITIONS_MODAL_TITLE; $this->description = MODULE_CONTENT_FOOTER_CONDITIONS_MODAL_DESCRIPTION; if ( defined('MODULE_CONTENT_FOOTER_CONDITIONS_MODAL_STATUS') ) { $this->sort_order = MODULE_CONTENT_FOOTER_CONDITIONS_MODAL_SORT_ORDER; $this->enabled = (MODULE_CONTENT_FOOTER_CONDITIONS_MODAL_STATUS == 'True'); } } function execute() { global $PHP_SELF, $oscTemplate, $language; ob_start (); include('/includes/modules/content/' . $this->group . '/templates/conditions_modal.php'); $template = ob_get_clean(); if (tep_not_null(MODULE_CONTENT_FOOTER_CONDITIONS_MODAL_PAGES)) { $pages_array = array(); foreach (explode(';', MODULE_CONTENT_FOOTER_CONDITIONS_MODAL_PAGES) as $page) { $page = trim($page); if (!empty($page)) { $pages_array[] = $page; } } if (in_array(basename($PHP_SELF), $pages_array)) { $oscTemplate->addContent($template, $this->group); } } } 1) In lines 2 and 3, I've seen other modules leave these blank. Which is the recommend option, and why? 2) In line 9, I've seen it where the function is named (like here). I've also seen it where that line is replaced with: function __construct() { Which is better, and why? 3) Are there any other things I could do to improve (and learn from) this module? TIA! Malcolm Link to comment Share on other sites More sharing options...
burt Posted July 19, 2018 Share Posted July 19, 2018 It would be so cool if more people would ask these sorts of questions instead of just doing what they think is right. Kudos to you. which are lines 2 and 3 ? use basename(__FILE__) to call the template, and rename the template file appropriately (see code in point 4) the first function name in a class cannot be the same name as the "parent" class name (in php 7) use __construct or put an empty function in as so; function blah() { } You are displaying here: if (in_array(basename($PHP_SELF), $pages_array)) { $oscTemplate->addContent($template, $this->group); } so you might as well load the whole thing here; if (in_array(basename($PHP_SELF), $pages_array)) { ob_start (); include('/includes/modules/content/' . $this->group . '/templates/tpl_' . basename(__FILE__)); $template = ob_get_clean(); $oscTemplate->addContent($template, $this->group); } Link to comment Share on other sites More sharing options...
ArtcoInc Posted July 19, 2018 Author Share Posted July 19, 2018 @burt Thanks! 14 minutes ago, burt said: which are lines 2 and 3 ? var $code = 'cm_footer_conditions_modal'; var $group = 'footer_scripts'; Malcolm Link to comment Share on other sites More sharing options...
burt Posted July 19, 2018 Share Posted July 19, 2018 These two things let tthe script know what it is and where it should be. Link to comment Share on other sites More sharing options...
ArtcoInc Posted July 19, 2018 Author Share Posted July 19, 2018 1 minute ago, burt said: These two things let tthe script know what it is and where it should be. I thought something like that, but I see a lot of modules that leave this blank. That's why I asked. Thanks again! Link to comment Share on other sites More sharing options...
burt Posted July 19, 2018 Share Posted July 19, 2018 They can be blank, as they are extended in the __construct function. Look at the first 6 lines under the class cm_footer_conditions_modal { and The 6 $this-> in the your cm_footer_conditions_modal() / __construct function. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.