How to organize PHP code?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bruce W...1

    How to organize PHP code?

    I've been learning about PHP for a couple of weeks. With includes, PHP
    scripts, and HTML I can see where a large and complex website could
    easily turn in to a big hairy mess with files all over the place.

    Are there any adopted standards, recognized recommendations , or best
    practices on how all the code should be organized? I haven't found any
    websites that discuss this.

    Can anyone point me to information on this? If not then what do you do,
    i.e. if you are organized?

    Thanks for your help.
  • AndyJ

    #2
    Re: How to organize PHP code?

    I have not found one definative way, but read equally good things
    about minimal (code-seperate) and maximal (code-inline) styles.

    Personally for small pieces I put them inline.
    For repeating pieces I create functions and store in seperate file
    using include() when neccessary, grouping like functions together.

    Thats pretty much how I organise my projects. Inline is useful for
    display stuff and calculations should be done in functions IMHO.

    Hope that helps,

    Andu Turner.


    On Tue, 30 Sep 2003 17:32:40 -0400, "Bruce W...1"
    <bruce@noDirect Email.com> wrote:
    [color=blue]
    >I've been learning about PHP for a couple of weeks. With includes, PHP
    >scripts, and HTML I can see where a large and complex website could
    >easily turn in to a big hairy mess with files all over the place.
    >
    >Are there any adopted standards, recognized recommendations , or best
    >practices on how all the code should be organized? I haven't found any
    >websites that discuss this.
    >
    >Can anyone point me to information on this? If not then what do you do,
    >i.e. if you are organized?
    >
    >Thanks for your help.[/color]

    Comment

    • Pieter Linde

      #3
      Re: How to organize PHP code?


      You can use templates, have a look at this site.


      On Tue, 30 Sep 2003 17:32:40 -0400, Bruce W...1 wrote:
      [color=blue]
      > I've been learning about PHP for a couple of weeks. With includes, PHP
      > scripts, and HTML I can see where a large and complex website could
      > easily turn in to a big hairy mess with files all over the place.
      >
      > Are there any adopted standards, recognized recommendations , or best
      > practices on how all the code should be organized? I haven't found any
      > websites that discuss this.
      >
      > Can anyone point me to information on this? If not then what do you do,
      > i.e. if you are organized?
      >
      > Thanks for your help.[/color]

      Comment

      • Randell D.

        #4
        Re: How to organize PHP code?


        "Bruce W...1" <bruce@noDirect Email.com> wrote in message
        news:3F79F678.D 77F4ABC@noDirec tEmail.com...[color=blue]
        > I've been learning about PHP for a couple of weeks. With includes, PHP
        > scripts, and HTML I can see where a large and complex website could
        > easily turn in to a big hairy mess with files all over the place.
        >
        > Are there any adopted standards, recognized recommendations , or best
        > practices on how all the code should be organized? I haven't found any
        > websites that discuss this.
        >
        > Can anyone point me to information on this? If not then what do you do,
        > i.e. if you are organized?
        >
        > Thanks for your help.[/color]

        I read an old C++ book years ago and since the programming is structured
        similar, I've carried some of its suggestions with me... One being any
        functions I write are rarely ever larger than a screen shot/display (ie no
        need to scrool up/down to view a single function). Functions help you
        locate bugs and, while I am not 100% certain of this, I do believe that
        functions make better memory usage. This is because once a function
        returns/completes, its allocated memory is made free again. The only
        difference to this rule is if your function has been passed data, processed
        and returned in near completion.

        Secondly, I'm working on a project at the moment and other than inline print
        statements (for example, to print values in to form fields that were
        restored from my database) I have all my functions in a seperate file that
        gets included before HTML manages to kicks in. This is in part because of
        headers which much be sent before any html is sent - thus if you want to use
        cookies, delivered by PHP, if you try to set a cookie after a piece of html
        code you'll run in to errors.

        Thus, for ever html file that I have, I also have a seperate php file which
        gets included...

        Oh! and lastly - if its your first time programming... make great usage of
        remark statements in your code so when you reinspect it in weeks or months
        time, or if someone else is working on your code, that it gives you/them an
        easier life having to see what its supposed to be doing.

        Hope that helps...


        Comment

        • rush

          #5
          Re: How to organize PHP code?

          "Bruce W...1" <bruce@noDirect Email.com> wrote in message
          news:3F79F678.D 77F4ABC@noDirec tEmail.com...[color=blue]
          > I've been learning about PHP for a couple of weeks. With includes, PHP
          > scripts, and HTML I can see where a large and complex website could
          > easily turn in to a big hairy mess with files all over the place.[/color]

          Maybe you could use some template system to keep your html an php separate,
          it can help a lot to clean the code. TemplateTamer wiki has a number of
          examples that wil get you started, and you can also look at other template
          engines. (try searching in google for php template.

          rush
          --
          Get your very own domain easily. Fast and professional customer service.




          Comment

          • Justin Koivisto

            #6
            Re: How to organize PHP code?

            Bruce W...1 wrote:[color=blue]
            > I've been learning about PHP for a couple of weeks. With includes, PHP
            > scripts, and HTML I can see where a large and complex website could
            > easily turn in to a big hairy mess with files all over the place.[/color]

            been there, done that.
            [color=blue]
            > Are there any adopted standards, recognized recommendations , or best
            > practices on how all the code should be organized? I haven't found any
            > websites that discuss this.[/color]

            Nor have I.
            [color=blue]
            > Can anyone point me to information on this? If not then what do you do,
            > i.e. if you are organized?[/color]

            Over the years I have developed a library of functions and classes I've
            used in project before. In that last year, I started to create myself a
            CMS system to organize all this stuff and make me more efficient at
            putting things together.

            The way I organize things now is as follows:

            config.ini.php: This file holds all the settings that I will be using
            throughout the project. (Database connection details, image sizes, email
            headers, etc.)

            template.php: Parses config.ini.php and creates a configuration array,
            initializes the database connection through db abstraction (usually
            Metabase), and sets up an object of a class that holds the database
            connection as well as some other helpful functions (like a formatHTML
            function that pulls from the database, searches for HTML/non-HTML and
            combines them for display). This file also parses the URI and/or
            QueryString to find where in the database to look for the information.
            Depending on what that URI/QueryString needs, other files may be
            included. output buffering is used to store all the page's output into a
            collection of variables.

            include/*: This directory holds a collection of files that will be
            included in the project somewhere. For instance, in my CMS, you will
            find files like: normal.inc.php, news.inc.php, staff.inc.php, etc. - one
            for each type of page supported by the CMS. There are also files like
            update.normal.i nc.php, etc. - update functions for each of the page
            types. You would also find files like form.date.start .php - a file that
            looks for a certain date variable, parses it, and creates 3 select menus
            (year, month, day).

            _site_component s/*: In this directory I keep things like the db
            abstraction package files, my security system used for the CMS, a folder
            for images used on the site, a folder for each css definitions, js
            scripts, flash movies, etc.

            design.php: This file is included at the end of the template.php file.
            This holds the HTML design of the project. Generated displays are then
            echo'd where they are needed.

            This approach keeps my HTML and PHP separate. Additionally, it keeps
            closely-related control procedures and functions separate from others.
            By doing this, I don't have all the code included into all requests,
            only the parts that are needed. (Actually, my main class isn't separated
            like that, but I've been thinking of doing so.)

            Just my $.02

            --
            Justin Koivisto - spam@koivi.com
            PHP POSTERS: Please use comp.lang.php for PHP related questions,
            alt.php* groups are not recommended.

            Comment

            • William L. Berggren

              #7
              Re: How to organize PHP code?

              "Bruce W...1" <bruce@noDirect Email.com> wrote in message news:<3F79F678. D77F4ABC@noDire ctEmail.com>...[color=blue]
              > I've been learning about PHP for a couple of weeks. With includes, PHP
              > scripts, and HTML I can see where a large and complex website could
              > easily turn in to a big hairy mess with files all over the place.
              >
              > Are there any adopted standards, recognized recommendations , or best
              > practices on how all the code should be organized? I haven't found any
              > websites that discuss this.
              >
              > Can anyone point me to information on this? If not then what do you do,
              > i.e. if you are organized?
              >
              > Thanks for your help.[/color]

              I would suggest looking a pear php coding standards. Basically use 4
              spaces to indent code within brackets. Yes, things can get messy.
              But, for me each page only requires 3 files. A main, template and a
              table function. The template is reused for all pages.

              Comment

              • Michael Willcocks

                #8
                Re: How to organize PHP code?

                I have only caught the last few lines of this thread (sorry if this has been
                spoken about)

                Is there software out there to format your code. as in add tabs where it
                needs tabs, delete trailing spaces etc?

                I use editplus and it does a very simple manual code format..


                tia

                "William L. Berggren" <bill.berggren@ padobe.com> wrote in message
                news:290c5e5f.0 310011816.38155 bb7@posting.goo gle.com...[color=blue]
                > "Bruce W...1" <bruce@noDirect Email.com> wrote in message[/color]
                news:<3F79F678. D77F4ABC@noDire ctEmail.com>...[color=blue][color=green]
                > > I've been learning about PHP for a couple of weeks. With includes, PHP
                > > scripts, and HTML I can see where a large and complex website could
                > > easily turn in to a big hairy mess with files all over the place.
                > >
                > > Are there any adopted standards, recognized recommendations , or best
                > > practices on how all the code should be organized? I haven't found any
                > > websites that discuss this.
                > >
                > > Can anyone point me to information on this? If not then what do you do,
                > > i.e. if you are organized?
                > >
                > > Thanks for your help.[/color]
                >
                > I would suggest looking a pear php coding standards. Basically use 4
                > spaces to indent code within brackets. Yes, things can get messy.
                > But, for me each page only requires 3 files. A main, template and a
                > table function. The template is reused for all pages.[/color]


                Comment

                • Bruce W...1

                  #9
                  Re: How to organize PHP code?

                  "Randell D." wrote:[color=blue]
                  >
                  > I read an old C++ book years ago and since the programming is structured
                  > similar, I've carried some of its suggestions with me... One being any
                  > functions I write are rarely ever larger than a screen shot/display (ie no
                  > need to scrool up/down to view a single function). Functions help you
                  > locate bugs and, while I am not 100% certain of this, I do believe that
                  > functions make better memory usage. This is because once a function
                  > returns/completes, its allocated memory is made free again. The only
                  > difference to this rule is if your function has been passed data, processed
                  > and returned in near completion.
                  >
                  > Secondly, I'm working on a project at the moment and other than inline print
                  > statements (for example, to print values in to form fields that were
                  > restored from my database) I have all my functions in a seperate file that
                  > gets included before HTML manages to kicks in. This is in part because of
                  > headers which much be sent before any html is sent - thus if you want to use
                  > cookies, delivered by PHP, if you try to set a cookie after a piece of html
                  > code you'll run in to errors.
                  >
                  > Thus, for ever html file that I have, I also have a seperate php file which
                  > gets included...
                  >
                  > Oh! and lastly - if its your first time programming... make great usage of
                  > remark statements in your code so when you reinspect it in weeks or months
                  > time, or if someone else is working on your code, that it gives you/them an
                  > easier life having to see what its supposed to be doing.
                  >
                  > Hope that helps...[/color]
                  =============== =============== =============== =

                  That makes a lot of sense. I'm coming from experience with ASP.NET
                  where each page has one code-behind file. And common settings like a
                  database connection string are kept in a web.config file.

                  The web.config file would be equivalent to Justin's config.ini.php
                  further down in this thread.

                  This general paradigm is easily done with PHP, and for that I'm glad to
                  see.

                  Comment

                  • equus

                    #10
                    Re: How to organize PHP code?

                    This structure works for me...

                    /webroot
                    /bin (all binary files, mostly images)
                    /cron (all php & shell cron scripts relating to the current site)
                    /dev (scripts and images under development - this is a mess)
                    /doc (documentation, mostly small text files explaining stuff. also
                    contains change log. i go here when i forget what i last did)
                    /etc (configuration, i.e. database un&pw, etc - restricted perms)
                    /export (database dumps, usually with a cron)
                    /home (this is where most of the website lives)
                    /inc (included files)
                    /func (included functions)
                    /class (included objects)
                    index.php

                    So in the webroot there is just one file, index.php. All others are
                    categorized. Of course subdirectories may be required. For instance, I
                    usually chuck icon images into an /icons folder under /bin. It works for
                    me :)

                    It's funny that everyone I taught PHP to also use this structure or a
                    similar variation. I have seen some using a /scratch folder for pages that
                    won't live on the site for long, maybe an info gathering form for
                    instance. There's a robots.txt that prevents the pages from being indexed.

                    ../Albe

                    On Tue, 30 Sep 2003 17:32:40 -0400, Bruce W...1 wrote:
                    [color=blue]
                    > I've been learning about PHP for a couple of weeks. With includes, PHP
                    > scripts, and HTML I can see where a large and complex website could
                    > easily turn in to a big hairy mess with files all over the place.
                    >
                    > Are there any adopted standards, recognized recommendations , or best
                    > practices on how all the code should be organized? I haven't found any
                    > websites that discuss this.
                    >
                    > Can anyone point me to information on this? If not then what do you do,
                    > i.e. if you are organized?
                    >
                    > Thanks for your help.[/color]

                    Comment

                    • William L. Berggren

                      #11
                      Re: How to organize PHP code?

                      "Michael Willcocks" <michael@metrog roup.com.au> wrote in message news:<3f7b9988$ 0$95044$c30e37c 6@lon-reader.news.tel stra.net>...[color=blue]
                      > I have only caught the last few lines of this thread (sorry if this has been
                      > spoken about)
                      >
                      > Is there software out there to format your code. as in add tabs where it
                      > needs tabs, delete trailing spaces etc?
                      >
                      > I use editplus and it does a very simple manual code format..
                      >
                      >
                      > tia[/color]


                      I use Kate text editor. It highlights php. It is a KDE linux
                      program.
                      It is hard to explain but Linux Format magazine rates
                      it a high 9 for text editors. It also has text-to-speech
                      capabilities, spell check and some other weird stuff. But the best
                      part is you can have 10 files
                      open at once and cut and paste between them.

                      Comment

                      Working...