Which way is better ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • StinkFinger

    Which way is better ?

    All,
    Which way below is the better method ? Basically A.PHP calls a function from
    B.PHP that contains a mess of db variables (i.e. fields, table names, etc.).
    The variables that are returned are used throughout the rest of the A.PHP
    script.
    I have left out the contents of $a,$b and $c and changed the function names
    and variable names for a more simple posting.

    a.php
    -----
    db_function(); //from b.php

    b.php
    ------
    function db_function ()
    {
    global $a, $b, $c;
    $a = ""; //fields
    $b = ""; //table
    $c = ""; //id
    }

    or this way...

    a.php
    -----
    list ($a, $b, $c) = db_function();

    b.php
    ------
    function db_function ()
    {
    $a = ""; //fields
    $b = ""; //table
    $c = ""; //id
    return array ($a, $b, $c);
    }

    Many thanks.


  • Andy Hassall

    #2
    Re: Which way is better ?

    On Sat, 3 Apr 2004 13:08:09 -0900, "StinkFinge r" <stinky@pinky.c om> wrote:
    [color=blue]
    >Which way below is the better method ? Basically A.PHP calls a function from
    >B.PHP that contains a mess of db variables (i.e. fields, table names, etc.).
    >The variables that are returned are used throughout the rest of the A.PHP
    >script.
    >I have left out the contents of $a,$b and $c and changed the function names
    >and variable names for a more simple posting.
    >
    >a.php
    >-----
    >db_function( ); //from b.php
    >
    >b.php
    >------
    >function db_function ()
    >{
    > global $a, $b, $c;
    > $a = ""; //fields
    > $b = ""; //table
    > $c = ""; //id
    >}
    >
    >or this way...
    >
    >a.php
    >-----
    >list ($a, $b, $c) = db_function();
    >
    >b.php
    >------
    >function db_function ()
    >{
    > $a = ""; //fields
    > $b = ""; //table
    > $c = ""; //id
    > return array ($a, $b, $c);
    >}[/color]

    The general rule is avoid globals unless you have a good reason for it. So the
    second one, probably.

    --
    Andy Hassall <andy@andyh.co. uk> / Space: disk usage analysis tool
    http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space

    Comment

    • StinkFinger

      #3
      Re: Which way is better ?

      Thanks - I was putting some new found knowledge to work (i.e. from PHP
      Visual Blueprint) and the first way was my original way, the second, ideas
      taken from the book.

      "Andy Hassall" <andy@andyh.co. uk> wrote in message
      news:ardu60h69s 3od4lfteha3k5jn qbek2achs@4ax.c om...[color=blue]
      > On Sat, 3 Apr 2004 13:08:09 -0900, "StinkFinge r" <stinky@pinky.c om> wrote:
      >[color=green]
      >>Which way below is the better method ? Basically A.PHP calls a function
      >>from
      >>B.PHP that contains a mess of db variables (i.e. fields, table names,
      >>etc.).
      >>The variables that are returned are used throughout the rest of the A.PHP
      >>script.
      >>I have left out the contents of $a,$b and $c and changed the function
      >>names
      >>and variable names for a more simple posting.
      >>
      >>a.php
      >>-----
      >>db_function() ; //from b.php
      >>
      >>b.php
      >>------
      >>function db_function ()
      >>{
      >> global $a, $b, $c;
      >> $a = ""; //fields
      >> $b = ""; //table
      >> $c = ""; //id
      >>}
      >>
      >>or this way...
      >>
      >>a.php
      >>-----
      >>list ($a, $b, $c) = db_function();
      >>
      >>b.php
      >>------
      >>function db_function ()
      >>{
      >> $a = ""; //fields
      >> $b = ""; //table
      >> $c = ""; //id
      >> return array ($a, $b, $c);
      >>}[/color]
      >
      > The general rule is avoid globals unless you have a good reason for it. So
      > the
      > second one, probably.
      >
      > --
      > Andy Hassall <andy@andyh.co. uk> / Space: disk usage analysis tool
      > http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space[/color]


      Comment

      • FLEB

        #4
        Re: Which way is better ?

        Regarding this well-known quote, often attributed to StinkFinger's famous
        "Sat, 3 Apr 2004 13:08:09 -0900" speech:
        [color=blue]
        > All,
        > Which way below is the better method ? Basically A.PHP calls a function from
        > B.PHP that contains a mess of db variables (i.e. fields, table names, etc.).
        > The variables that are returned are used throughout the rest of the A.PHP
        > script.
        > I have left out the contents of $a,$b and $c and changed the function names
        > and variable names for a more simple posting.
        >
        > a.php
        > -----
        > db_function(); //from b.php
        >
        > b.php
        > ------
        > function db_function ()
        > {
        > global $a, $b, $c;
        > $a = ""; //fields
        > $b = ""; //table
        > $c = ""; //id
        > }
        >
        > or this way...
        >
        > a.php
        > -----
        > list ($a, $b, $c) = db_function();
        >
        > b.php
        > ------
        > function db_function ()
        > {
        > $a = ""; //fields
        > $b = ""; //table
        > $c = ""; //id
        > return array ($a, $b, $c);
        > }
        >
        > Many thanks.[/color]

        I'd recommend using a class. OOP would apply very well to this.

        --
        -- Rudy Fleminger
        -- sp@mmers.and.ev il.ones.will.bo w-down-to.us
        (put "Hey!" in the Subject line for priority processing!)
        -- http://www.pixelsaredead.com

        Comment

        Working...