SitePoint Sponsor

User Tag List

Page 1 of 4 1234 LastLast
Results 1 to 25 of 81
  1. #1
    Patience... bronze trophy solidcodes's Avatar
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    911
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    Arrow How do you organize your PHP scripts and files?

    Actually i want my PHP codes to be professionally organize,
    Yeah, i can understand OOP, but how do you organize them properly?

    Let say i have a very very BIG project, now what do you do for your first step
    to organize your php script? then the second step,the third step and so on.

    I want to hear your experiences.

    How do you also organize the files and folders?


    Thanks in advance.

  2. #2
    SitePoint Evangelist BJ Duncan's Avatar
    Join Date
    Jun 2007
    Location
    Bowen Mountain, NSW
    Posts
    490
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    As much as I love computer technology, nothing beats good old fashioned pen and paper.Before I even touch a keyboard I put everything on paper.

    Much like database method I organise things into segments / sections generally by definition of category, depending on the particular project and the clients needs.
    Regards,
    BJ Duncan

  3. #3
    SitePoint Enthusiast
    Join Date
    Aug 2007
    Location
    edge of nowhere
    Posts
    74
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I keep my things one class per file, names className.class.php, similar classes grouped in a subfolder (if there are more than one), all subfolders in a classes folder which I place in the include path.
    If I have reoccuring code in files I put that in a common, loader-type file, which I then include everywhere. This sits in a folder of it's own, again in the include path.
    Everything related to configuring a script is confined to one array, say $params['this_or_that'], which sits in a config file of it's own, which sits in the include path.

    May be difficult to organize at first, but once you have everything figured out, like a blank project it's a simple copy-paste job to start a new one, comment out, or delete what you don't need from the current job in the loader file and start coding.
    Programming boils down to three things: fast, good and cheap.
    Please pick two.

  4. #4
    SitePoint Member
    Join Date
    Mar 2005
    Location
    Lithuania
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Take a look at how zend framework is organised. I'm using something very similar - one file per class. Classes are categorised by their types - modules, controllers, views etc. Every `category` has its own subfolder in a class directory. As sebulbus said, every configurable thing must be put into one config file. I'm using constants for such things.
    Now about a script, try to read some articles about MVC pattern. In a few ford it's a way of programming, when data, it's presentation and logic are separated. Of course, don't forget about OOP principles and always think about what can be reused in other projects, make use of inherintance.

  5. #5
    SitePoint Addict telos's Avatar
    Join Date
    Mar 2005
    Location
    192.168.2.34
    Posts
    279
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Typically, I don't put everything in one class. I organize it into smaller classes. for example:
    class.MySQL.php
    class.TextArea.php
    class.DropDown.php
    class.SMTP.php
    class.Login.php
    class.Etc.php

    I store all of these in a folder like: lib/php/classes

    Then, I use autoloading so that I only include the classes I'm using for that particular script. For example, I don't need to include the MySQL class if I'm not going to make a MySQL query on this page.

    Once autoloading is set up, all I need to do is:
    PHP Code:
    $t = new TextArea($var1$var2$etc); 
    Obviously, not all classes will be simple enough to implement in one line. But hopefully you see my point.

    Study the PHP classes really good before starting: http://php.net/class
    Last edited by telos; Aug 27, 2007 at 10:00.

  6. #6
    Worship the Krome kromey's Avatar
    Join Date
    Sep 2006
    Location
    Fairbanks, AK
    Posts
    1,621
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, I now use MVC using the Zend Framework, so my organization pretty much follows the conventions of ZF now.

    Before that, I'd organize all my scripts by what their purpose was:
    / - the root of the application (not necessarily the server's web root)
    /bin - all my data processing scripts
    /img - images and PHP scripts that produced images would go here
    /inc - include files; only config scripts would be in the root of this subdirectory
    /inc/classes - my class files (one class per file)
    /inc/func - function files, grouped by type (i.e. similar functions in one file)
    /template - my template files

    All the scripts that the user would ever see would be in the application's root; while for particularly large projects this sometimes got a little out of hand, for most projects this organization worked very well for me.
    PHP questions? RTFM
    MySQL questions? RTFM

  7. #7
    Patience... bronze trophy solidcodes's Avatar
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    911
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    you guys are really advanced.

    thanks guys.

  8. #8
    SitePoint Wizard
    Join Date
    Jul 2004
    Location
    Minneapolis, MN
    Posts
    1,924
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    MVC. I also do not use abbreviations for directory titles, so I have things like "include" and "library."

  9. #9
    Beer drinker Srirangan's Avatar
    Join Date
    Jan 2005
    Location
    Beerland!
    Posts
    776
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    /engine/ stores my controller classes, the business logic of the particular app names the model while the theme is usually also the name of the view.
    Online Startups Insight for new entrepreneurs

  10. #10
    phpLD Fanatic bronze trophy dvduval's Avatar
    Join Date
    Mar 2002
    Location
    Silicon Valley
    Posts
    3,627
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Do any of you guys ever try to reorganize existing projects that were started by someone else?

  11. #11
    Working on it... Contrid's Avatar
    Join Date
    Apr 2006
    Location
    Online
    Posts
    955
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Get an MVC framework.
    It's genius...no, it's beautifully genius.
    It does all the work for you.

    CakePHP :
    Creating applications this way will win you peace, honor, women, and money beyond even your wildest fantasies. Simple, isn't it?
    And so I got lost in code...completely asphyxiated by it...

    Premium WordPress plugins - Tribulant Software

  12. #12
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    Plano
    Posts
    643
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i do my OOP design as others have mentioned, as for my file structure, i do something like this:

    root/
    -backup/
    --database/
    --filestystem/
    --mail/
    -classes/
    --controllers/
    -templates/
    --html/
    --email/
    --rss/
    --(etc)
    -www/
    --images/
    --styles/

    all classes get their own file, and i create subfolders based on whether i need it or not for the current project, but this is just a very minimal base for my projects.

  13. #13
    An average geek earl-grey's Avatar
    Join Date
    Mar 2005
    Location
    Ukraine
    Posts
    1,403
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Using CakePHP here:
    Code:
    /cake - CakePHP libraries
    /app/models - models, each model is in its own file
    /app/views - views, each controller has its own subdirectory
    /app/controllers - controllers, each controller is in its own file
    /app/vendors - other classes and functions, file per class/library
    /app/config - configuration:
      /core.php - CakePHP and application-specific configuration
      /database.php - database connection configuration

  14. #14
    SitePoint Member handry's Avatar
    Join Date
    Nov 2005
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    yes

    MVC is the best structured program coding for PHP Scripts.

    Try www.codeigniter.com

    That is the best user guide that cut off your learning curve.

  15. #15
    Beer drinker Srirangan's Avatar
    Join Date
    Jan 2005
    Location
    Beerland!
    Posts
    776
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code Igniter is a very promising project. It doesn't overdo like most other PHP Frameworks.
    Online Startups Insight for new entrepreneurs

  16. #16
    SitePoint Enthusiast
    Join Date
    Feb 2007
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I like to have a folder structure that mirrors the class hierarchy, sort of like is suggested in the PEAR coding standards (but not quite exactly like that, since I kind of feel like it's overkill for a non-PEAR project). Since I don't usually have too many levels of inheritance, it works sort of like this (folders in square brackets, files otherwise):

    [classes]
    --[Model]
    ----ConcreteModel1.class.php
    ----ConcreteModel2.class.php
    --[Controller]
    ----ConcreteController1.class.php
    ----ConcreteController2.class.php
    --Model.class.php
    --Controller.class.php

    One thing, however, doesn't seem to fit very well into this system: interfaces! Since a class can implement more than one interface, it's not really possible to store classes in folders based on their interfaces alone. So where do the interface files go?

    So far my answer has always been to put them in a separate folder independent of the [classes] folder. I.e.,

    [classes]
    --(see above for structure)
    [interfaces]
    --iModel.php
    --iController.php

    This seems kind of messy to me, but I haven't had any better ideas yet. Any suggestions?

  17. #17
    SitePoint Evangelist
    Join Date
    Feb 2005
    Posts
    581
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I usually do one class/interface/function per file using the name as the file plus ".php". I don't use ".class.php" or anything like that because if "class" suddenly becomes "interface" I don't want to have to keep renaming my files (just being lazy I guess).

    As far as directory structure goes:
    Code:
    root
      public
        stylesheets
        images
        javascripts
        [etc.]
      private
        app
          models
          views
          controllers
          helpers
        lib
          [Basic framework classes, abstract classes, interfaces, etc]
        db
          [SQL files, XML files, etc]
          SQLite
            [SQLite database (*.db) files]
        config
          [YAML, XML, INI, etc files - one per area/category (main.yml, db.yml, etc)]
        tests
        contrib
          [Libraries/code that I did not write]
      temp
         cache
         logs
         sessions
    The directory structure is pretty much a modified version of the one RoR uses.
    I will not flame the newbies,
    I will not flame the newbies,
    I will flame the newbies...
    Table free is the way to be!

  18. #18
    SitePoint Zealot Dachande663's Avatar
    Join Date
    Feb 2005
    Location
    Birmingham, UK
    Posts
    151
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    By comparison I keep mine pretty simple:
    /
    -func
    -views
    -templates
    -thirdparty

    and that's pretty much it.
    Web Developer & Geek: hybridlogic.co.uk ~ lukelanchester.com

  19. #19
    SitePoint Enthusiast NextSite's Avatar
    Join Date
    Jul 2006
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I like Code Igniter as well, and want to get into it, I haven't learned it yet, but the tutorials are nice and easy to learn.
    http://www.codeigniter.com

    I've also heard good things about CakePHP so might check that out as well.
    http://www.cakephp.org

  20. #20
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    CI and Cake are trash compared to Symfony.

    Code:
    apps/
      [app name]/
        modules/
          [module name]/
              actions/
              config/
              lib/
              templates/
              validate/
    batch/
    cache/
    config/
    data/
      sql/
    doc/
    lib/
      model/
    log/
    plugins/
    test/
      unit/
      functional/
    web/
      css/
      images/
      js/
      uploads/

  21. #21
    SitePoint Enthusiast
    Join Date
    Sep 2005
    Posts
    55
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I do literally exactly the same as Dachande663

    Keep it very simple
    Aaron Luckie - Paid search and SEO blog

  22. #22
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wow, great thread; I'm right in the middle of a massive script reorganization project right now. Unfortunately, I scarcely understand anything people are suggesting on this thread. Guess I need another two years of PHP experience.

    But I basically organize my scripts according to function and location - i.e., do they affect something in the head section, the top of the page, the body, the "central body" where I display articles and images, or whatever?

    I'm trying to develop a system that allows several websites to share a central repository of PHP scripts. Every website uses the same footer, which is all mapped out on a single file, footer.php. But websites with unique features obviously require unique scripts.

    But I'm trying to standardize things as much as possible. For example, each website will have a Topics section, with the main Topics page therefore linked to the same file. However, articles will be stored in separate database tables for each website. Therefore, I substitute a variable for the table name in my query, then use a simple PHP switch to input the correct table name...

    PHP Code:
    switch($Site)
     case 
    'MySite1':
     
    $Table 'MySite1';
     break;
     case 
    'MySite2':
     
    $Table 'MySite2';
     break;
     default:
     break;

    Do you have to understand OOP to get any use out of a program like CodeIgniter or Symfony?

  23. #23
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by geosite View Post
    PHP Code:
    switch($Site)
     case 
    'MySite1':
     
    $Table 'MySite1';
     break;
     case 
    'MySite2':
     
    $Table 'MySite2';
     break;
     default:
     break;

    Do you have to understand OOP to get any use out of a program like CodeIgniter or Symfony?
    I'll never understand why you bloat a simple conditional that could be done in a single line to an 11 line switch... anyway... no. These are all object oriented frameworks.

  24. #24
    SitePoint Evangelist tetsuo shima's Avatar
    Join Date
    Oct 2005
    Location
    Switzerland
    Posts
    597
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I follow the ZF file organization, which is very clear to me, even for my own framework.
    The SEO Faq thread
    Dependency injection made easy: Phemto

  25. #25
    SitePoint Evangelist
    Join Date
    Jun 2004
    Location
    California
    Posts
    440
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by tetsuo shima View Post
    I follow the ZF file organization, which is very clear to me, even for my own framework.
    Agreed. I follow the ZF naming, coding, and testing conventions. It keeps my projects clean and uniform even if I'm not using the ZF.
    Happy switcher to OS X running on a MacBook Pro.

    Zend Certified Engineer


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
  •