SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Member
    Join Date
    May 2013
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Arrow How to write better php code?

    Hi friends,
    I am just starting php. How to enrich my knowledge in PHP. Please help me to write better php code specially oops concept.

  2. #2
    SitePoint Addict
    Join Date
    Dec 2011
    Posts
    221
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    I can only advice three things.

    1. Practice
    2. Practice
    3. Practice

    read a lot.

  3. #3
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,403
    Mentioned
    45 Post(s)
    Tagged
    12 Thread(s)
    Hi Webiswide,

    I've some suggestions on how you might approach both topics:

    The PHP language
    If you want to get a deeper understanding of the language and learn about some best-practices, I've found http://www.phptherightway.com/ to be very useful, and it links to other tutorials and sources of good information.

    It's also worth spending some time browsing the PHP manual, to learn about the various built-in functions.. oftentimes you can find yourself trying to re-implement some piece of functionality that already exists in the language itself.

    OOP Concepts
    You can learn about the OOP features of PHP from the manual, but it won't really help you to learn how to write good OO code - for that, you can learn a great deal from looking at other peoples code. Github is great for this because it makes it easy to browse around the source code of thousands of different projects. Find some well known figures from the PHP community and checkout their github repositories, see what you can learn from their code.

    Most importantly, as claro says, is to practice. For many of us, the best way to really 'get' a concept is to build something using it. If you're reading up about PHP's session handling, write a small app to try out what you've learned.

    Lastly, browsing forums like this one lets you see the kind of problems people commonly run into and different ways of solving them.

  4. #4
    SitePoint Mentor bronze trophy
    John_Betong's Avatar
    Join Date
    Aug 2005
    Location
    City of Angels
    Posts
    1,834
    Mentioned
    73 Post(s)
    Tagged
    6 Thread(s)
    Quote Originally Posted by Webiswide View Post
    Hi friends,
    I am just starting php. How to enrich my knowledge in PHP. Please help me to write better php code specially oops concept.
    When developing on every project always include this common file ( can safely be used locally or online)

    #constants.php
    PHP Code:
    <?php 
      
    # set $path='your-project';
         
    ini_set('error_log'$path .'/php_error.php');

      
    defined('LOCALHOST') ?: define'LOCALHOST''localhost' === $_SERVER['SERVER_NAME'] ); 

      if (
    LOCALHOST):
        
    // following parameters should already be set in your php.ini
        
    error_reporting(-1); 
        
    ini_set('display_errors'1);

        
    # validate previous settings 
        
    if( -!== error_reporting() * ini_get'display_errors' ) ):
          echo 
    'Webiswide we have a problem :(';
          
    // phpinfo();
          
    die;  
        endif;
      else:  
        
    # ONLINE so do not show errors or warnings, etc
        # error_reporting(1); // maybe, up to you
        
    ini_set('display_errors'0);
      endif;

    # debugging function
    if ( LOCALHOST && (! function_exists('vd') )):
      
    //===================
      //
      //  usage:
      //        vd( $anyVariable );
      //        vd( $anyVariable, 0 );
      //
      //===================
      
    function vd($param$mode=1)
      {
        echo 
    '<pre>';
           if(
    $mode):
              
    print_r($param);
           else:
              
    var_dunp($param);
           endif;
        echo 
    '</pre>';
      }
    endif;
    Always check the value of a function otherwise one day you will get bitten

    Just received this PHP email:
    http://www.phpclasses.org/blog/packa...-possible.html
    Last edited by John_Betong; May 29, 2013 at 06:20. Reason: added error_log and 10 Tips, etc
    Learn how to be ready for The New Move to Discourse

    How to make Make Money Now with a *NEW* look

    Be sure to congratulate Patche on earning Member of the Month for July 2014

  5. #5
    SitePoint Zealot
    Join Date
    Aug 2006
    Posts
    164
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I would add setting timezone to that script. I think it is required since >= 5.3 and might lead to a lot of errors reported to the logs.
    http://www.yourshoutbox.com <- FREE chat for your website - limited offer
    http://www.smartchatbox.com <- let your visitors talk

  6. #6
    SitePoint Guru
    Join Date
    Nov 2003
    Location
    Huntsville AL
    Posts
    689
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by claro View Post
    I can only advice three things.

    1. Practice
    2. Practice
    3. Practice

    read a lot.
    As the great philosopher Buffy Summers once pointed out, that is one thing repeated three times.

  7. #7
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,629
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    It's actually repeated twice.
    Ian Anderson
    www.siteguru.co.uk


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
  •