Jump to content

Search the Community

Showing results for tags 'refactoring'.

  • Search By Tags

    Type tags separated by commas.
  • Search By Author

Content Type


Forums

  • Welcome to PHP Freaks
    • Announcements
    • Introductions
  • PHP Coding
    • PHP Coding Help
    • Regex Help
    • Third Party Scripts
    • FAQ/Code Snippet Repository
  • SQL / Database
    • MySQL Help
    • PostgreSQL
    • Microsoft SQL - MSSQL
    • Other RDBMS and SQL dialects
  • Client Side
    • HTML Help
    • CSS Help
    • Javascript Help
    • Other
  • Applications and Frameworks
    • Applications
    • Frameworks
    • Other Libraries
  • Web Server Administration
    • PHP Installation and Configuration
    • Linux
    • Apache HTTP Server
    • Microsoft IIS
    • Other Web Server Software
  • Other
    • Application Design
    • Other Programming Languages
    • Editor Help (PhpStorm, VS Code, etc)
    • Website Critique
    • Beta Test Your Stuff!
  • Freelance, Contracts, Employment, etc.
    • Services Offered
    • Job Offerings
  • General Discussion
    • PHPFreaks.com Website Feedback
    • Miscellaneous

Find results in...

Find results that contain...


Date Created

  • Start

    End


Last Updated

  • Start

    End


Filter by number of...

Joined

  • Start

    End


Group


AIM


MSN


Website URL


ICQ


Yahoo


Jabber


Skype


Location


Interests


Age


Donation Link

Found 3 results

  1. Imagine 6 PHP classes (one each for a product line), that have very similar coding structures, that go like this: //function that computes stuff inside each of 6 files: //they vary slightly from file to file but essentially it is this: function computeFunction { $this->x = new X(); $this->x->calcD(); if ($this->x->dOk) { $this->x->calcE(); $this->x->calcN(); } //more complicated logic that is essentially like above //and by the way! print $this->x->someVarThatIsUsedLater; } Then there is a single class like so : class X { function calcD() { //compute some condition if (<computed condition is met>) $this->dOk = true; else $this->dOk = false; //and by the way $this->someVarThatIsUsedLater = 4; } } Just to bring your attention to it, none of these functions return any result or value, but they nevertheless operate on variables of key interest via side-effects. That is, they modify variables that essentially act like globals, and then use those variables later ($this->dOk and $this->someVarThatIsUsedLater are one more prominent examples). I need to untangle this mess. And make it clean and clear again, and make sense. How do I best proceed? I have been wrestling with some ideas... like $this->dOk, can within reason be turned into a return variable of calcD() function, and then be tested against like if ($this->x->calcD()) and I think it will be reasonable enough. But then there are other functions that don't return anything and just act on variables via side-effects anyway so $this->dOk is one of the lesser troubles... Other than that, what I am thinking of doing is getting rid of these mini-functions (calcE(), calcN(), etc.), removing them as a funciton, and putting their body directly into the code, as a first step to refactor. Many of the computations done inside are just a few lines of code anyway, and the functions kind of hide a lot of side-effects that happen, instead of actually encapsulating the behavior. So while it may be counter-intuitive to dismantle the functions that appear to be doing something that normally can be encapsulated (computing key variables E, N, etc), I think dismantling them will actually clean things up as far as collecting all the side-effects inside a single parent function thereby making them more visible. Caveat: while doing so I will end up with 6 copies of untangled dismantled functions, because dismantling class X and putting its content into each of the 6 product line classes will have that effect. But my hope is that from that point I will see more clearly to start identifying places where I can start to truly encapsulating the behavior via various structures, instead of masking it. Problems / Questions: I would like to but I am not entirely sure that I can skip that step of dismantling functions & the 6x multiplying effect. It's probably the same like skipping steps in solving polynomial equations. Some can do it and some need to list each step of their work. And I am not entirely sure what structures I can replace it with in the end after I dismantle the functions. It also looks like a lot of work. Is there a better way? P.S. I already put tests on computeFunction() for each product line so I can be less paranoid about hacking stuff up.
  2. Hello there. I'm a web designer trying to learn more development skills. I've written this opening hours widget for wordpress. Its functionality is very simple which is all I really need and it does the job, however I'd love to know how to make the code smaller as there are lots of thing repeating. I think parts of it could be done using a for each loop and arrays, but I can't figure out what way to do it. If anyone can help or point me in the right direction for what topic specifically to study I would be very grateful. Please see code below. Sorry if its really badly written, /** * Creating and displaying an Opening Times Widget */ wp_register_sidebar_widget( 'opening_times_widget', // your unique widget id 'Opening Times', // widget name 'opening_times_widget_display', // callback function to display widget array( // options 'description' => 'This widget displays your opening hours in your website sidebar' ) ); wp_register_widget_control( 'opening_times_widget', // id 'opening_times_widget', // name 'opening_times_widget_control' // callback function ); function opening_times_widget_control($args=array(), $params=array()) { //the form is submitted, save into database if (isset($_POST['submitted'])) { update_option('opening_times_widget_title', $_POST['widgettitle']); update_option('opening_times_widget_email', $_POST['widgetemail']); update_option('opening_times_widget_description', $_POST['description']); update_option('opening_times_widget_monday', $_POST['mondayhours']); update_option('opening_times_widget_tuesday', $_POST['tuesdayhours']); update_option('opening_times_widget_wednesday', $_POST['wednesdayhours']); update_option('opening_times_widget_thursday', $_POST['thursdayhours']); update_option('opening_times_widget_friday', $_POST['fridayhours']); update_option('opening_times_widget_saturday', $_POST['saturdayhours']); update_option('opening_times_widget_sunday', $_POST['sundayhours']); } //load options $widgettitle = get_option('opening_times_widget_title'); $widgetemail = get_option('opening_times_widget_email'); $description = get_option('opening_times_widget_description'); $mondayhours = get_option('opening_times_widget_monday'); $tuesdayhours = get_option('opening_times_widget_tuesday'); $wednesdayhours = get_option('opening_times_widget_wednesday'); $thursdayhours = get_option('opening_times_widget_thursday'); $fridayhours = get_option('opening_times_widget_friday'); $saturdayhours = get_option('opening_times_widget_saturday'); $sundayhours = get_option('opening_times_widget_sunday'); ?> Widget Title: ( eg: Opening Hours )<br /> <input type="text" class="widefat" name="widgettitle" value="<?php echo stripslashes($widgettitle); ?>" /> <br /><br /> Monday<br /> <input type="text" class="widefat" name="mondayhours" value="<?php echo stripslashes($mondayhours); ?>" /> <br /><br /> Tuesday<br /> <input type="text" class="widefat" name="tuesdayhours" value="<?php echo stripslashes($tuesdayhours); ?>" /> <br /><br /> Wednesday<br /> <input type="text" class="widefat" name="wednesdayhours" value="<?php echo stripslashes($wednesdayhours); ?>" /> <br /><br /> Thursday<br /> <input type="text" class="widefat" name="thursdayhours" value="<?php echo stripslashes($thursdayhours); ?>" /> <br /><br /> Friday<br /> <input type="text" class="widefat" name="fridayhours" value="<?php echo stripslashes($fridayhours); ?>" /> <br /><br /> Saturday<br /> <input type="text" class="widefat" name="saturdayhours" value="<?php echo stripslashes($saturdayhours); ?>" /> <br /><br /> Sunday<br /> <input type="text" class="widefat" name="sundayhours" value="<?php echo stripslashes($sundayhours); ?>" /> <br /><br /> Additional Information:<br /> <textarea class="widefat" rows="5" name="description"><?php echo stripslashes($description); ?></textarea> <br /><br /> Email Address:<br /> <input type="text" class="widefat" name="widgetemail" value="<?php echo stripslashes($widgetemail); ?>" /> <br /><br /> <input type="hidden" name="submitted" value="1" /> <?php } function opening_times_widget_display($args=array(), $params=array()) { //load options $widgettitle = get_option('opening_times_widget_title'); $description = get_option('opening_times_widget_description'); $widgetemail = get_option('opening_times_widget_email'); $mondayhours = get_option('opening_times_widget_monday'); $tuesdayhours = get_option('opening_times_widget_tuesday'); $wednesdayhours = get_option('opening_times_widget_wednesday'); $thursdayhours = get_option('opening_times_widget_thursday'); $fridayhours = get_option('opening_times_widget_friday'); $saturdayhours = get_option('opening_times_widget_saturday'); $sundayhours = get_option('opening_times_widget_sunday'); //widget output echo stripslashes($args['before_widget']); echo stripslashes($args['before_title']); echo stripslashes($widgettitle).'<span class="opening-hours-icon"> </span>'; echo stripslashes($args['after_title']); echo '<div class="textwidget">'; if ($mondayhours != '') { echo '<div class="opening-hours-day-info">Monday: <span>'.stripslashes($mondayhours).'</span></div>'; } if ($tuesdayhours != '') { echo '<div class="opening-hours-day-info">Tuesday: <span>'.stripslashes($tuesdayhours).'</span></div>'; } if ($wednesdayhours != '') { echo '<div class="opening-hours-day-info">Wednesday: <span>'.stripslashes($wednesdayhours).'</span></div>'; } if ($thursdayhours != '') { echo '<div class="opening-hours-day-info">Thursday: <span>'.stripslashes($thursdayhours).'</span></div>'; } if ($fridayhours != '') { echo '<div class="opening-hours-day-info">Friday: <span>'.stripslashes($fridayhours).'</span></div>'; } if ($saturdayhours != '') { echo '<div class="opening-hours-day-info">Saturday: <span>'.stripslashes($saturdayhours).'</span></div>'; } if ($sundayhours != '') { echo '<div class="opening-hours-day-info">Sunday: <span>'.stripslashes($sundayhours).'</span></div>'; } echo '<div class="opening-hours-description">'.stripslashes(nl2br($description)).'</div>'; if ($widgetemail != '') { echo '<div class="opening-hours-email"><a href="mailto:'.stripslashes($widgetemail).'" target="_blank">Email us</a> to arrange an appointment</div>'; } echo '</div>';//close div.textwidget echo stripslashes($args['after_widget']); }
  3. Good morning everyone, I am trying to improve the speed of this website im working on and just improve my code in general. My pages are working, but sometimes I am getting connection errors to the database, database resides on the same server as the site, and I am aslo seeing slow load times. I am just trying to improve my code and learn the proper way to format everything, so if anyone has time please take a lot and leave some suggestions or anything you can to help. The site is www.tlgnewspaper.com if you want to take a look. <?php require_once("_includes/session.php");?> <?php require_once("_includes/connection.php");?> <?php require_once("_includes/functions.php"); ?> <?php include_once("_includes/formFunctions.php");?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>The Lafourche Gazette</title> <link type="text/css" href="_stylesheets/public.css" rel="stylesheet" /> <style type="text/css"> </style> <!--[if lt IE 9]>= <script type="text/javascript" src="_javascripts/modernizer.js"></script> <![endif]--> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script> <script type='text/javascript'>//<![CDATA[ $(window).load(function(){ (function() { function createPlayer(jqe, video, options) { var ifr = $('iframe', jqe); if (ifr.length === 0) { ifr = $('<iframe scrolling="no">'); ifr.addClass('player'); } var src = 'http://www.youtube.com/embed/' + video.id; if (options.playopts) { src += '?'; for (var k in options.playopts) { src += k + '=' + options.playopts[k] + '&'; } src += '_a=b'; } ifr.attr('src', src); jqe.append(ifr); } function createCarousel(jqe, videos, options) { var car = $('div.carousel', jqe); if (car.length === 0) { car = $('<div>'); car.addClass('carousel'); jqe.append(car); } $.each(videos, function(i, video) { options.thumbnail(car, video, options); }); } function createThumbnail(jqe, video, options) { var imgurl = video.thumbnails[0].url; var img = $('img[src="' + imgurl + '"]'); var desc; var container; if (img.length !== 0) return; img = $('<img align="left">'); img.addClass('thumbnail'); jqe.append(img); img.attr('src', imgurl); img.attr('title', video.title); img.click(function() { options.player(options.maindiv, video, $.extend(true, {}, options, { playopts: { autoplay: 1 } })); }); desk = $('<p class="yt-descript">' + video.title + '</p>'); jqe.append(desk); desk.click(function() { options.player(options.maindiv, video, $.extend(true, {}, options, { playopts: { autoplay: 1 } })); }); } var defoptions = { autoplay: false, user: null, carousel: createCarousel, player: createPlayer, thumbnail: createThumbnail, loaded: function() {}, playopts: { autoplay: 0, egm: 1, autohide: 1, fs: 1, showinfo: 1 } }; $.fn.extend({ youTubeChannel: function(options) { var md = $(this); md.addClass('youtube'); md.addClass('youtube-channel'); var allopts = $.extend(true, {}, defoptions, options); allopts.maindiv = md; $.getJSON('http://gdata.youtube.com/feeds/users/' + allopts.user + '/uploads?alt=json-in-script&format=5&callback=?', null, function(data) { var feed = data.feed; var videos = []; $.each(feed.entry, function(i, entry) { var video = { title: entry.title.$t, id: entry.id.$t.match('[^/]*$'), thumbnails: entry.media$group.media$thumbnail }; videos.push(video); }); allopts.allvideos = videos; allopts.carousel(md, videos, allopts); allopts.player(md, videos[0], allopts); allopts.loaded(videos, allopts); }); } }); })(); $(function() { $('#player').youTubeChannel({ user: 'tlgnewspaper' }); }); //]]> });//]]> function changeImage(a) { document.getElementById("img").src=a; } </script> <?php $CSS=1; require("calendar/calendar.php"); ?> </head> <body> <?php include("_includes/header.php");?> <section class = "wrapper"> <section class="col1"> <?php include("_includes/leftColumn.php");?> </section> <section class="col2"> <h1>Top News</h1> <?php include("_includes/topArticle.php");?> <?php include("_includes/newArticles.php");?> <section class = "gallery"> <h2>Newest Photos</h2> <h3>Click image to view the Gallery</h3> <div id="thumb_img" style="cursor:pointer"> <ul> <?php list ($title, $path, $description, $category) = getLastPhoto('school');?> <a><li onclick='changeImage("_images/_photo/<?php echo $path; ?>");'>School</li></a> <?php list ($title, $path, $description, $category) = getLastPhoto('clubs');?> <a><li onclick='changeImage("_images/_photo/<?php echo $path; ?>");'>Clubs</li></a> <?php list ($title, $path, $description, $category) = getLastPhoto('sports');?> <a><li onclick='changeImage("_images/_photo/<?php echo $path; ?>");'>Sports</li></a> <?php list ($title, $path, $description, $category) = getLastPhoto('user');?> <a><li onclick='changeImage("_images/_photo/<?php echo $path; ?>");'>User Submitted</li></a> </ul> </div>​ <div id="main_img" style="width:350px"> <?php list ($title, $path, $description, $category) = getLastPhoto('school');?> <a href="galleryChoice.php"><img id="img" width="350px" src="_images/_photo/<?php echo $path; ?>"></a> </div> </section> <section class = "test"> <h2>Video Gallery</h2> <div id="player"></div> </section> </section> <section class="col3"> <?php include("_includes/rightColumn.php");?> </section> </article> <?php include("_includes/footer.php");?>
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.