Can you share variables across objects created from the same class?

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

    Can you share variables across objects created from the same class?

    Hello all,
    Can someone please tell me if there is a way to declare variables
    within a class that can be shared among each new object created from
    this class?
    For instance I have the following:

    class DB {
    var $db;
    var $host;
    var $un;
    var $pw;
    var $connected;
    var $connections;

    function DB() {
    $host="myserver .com";
    $un="username";
    $pw="password";
    $this->db=null;
    }

    function Connect($databa se) {
    if(!$connected) {
    /* do your connection stuff….. */
    $connected=true ;
    $connections=1;
    }
    $this->db=$database ;
    $connections++;
    }
    }

    so that the variable $db which holds the name of the currently
    selected database will change with each instance of an object being
    created from this class, but the variable $connected and $connections
    will be shared between each instance of an object.

    Thank you in advance for your answers.
    Anthony
  • Zurab Davitiani

    #2
    Re: Can you share variables across objects created from the same class?

    Anthony Davis wrote on Tuesday 05 August 2003 09:56:
    [color=blue]
    > so that the variable $db which holds the name of the currently
    > selected database will change with each instance of an object being
    > created from this class, but the variable $connected and $connections
    > will be shared between each instance of an object.[/color]

    I may have misunderstood what you are saying, but I think a cleaner way to
    accomplish this would be to

    - create a ConnectionPool class which holds $connected, $connections and
    other related properties, and also has an array of DBConnection objects;
    - create a DBConnection class (similar to your DB class) and add an instance
    to the array in ConnectionPool for every new connection.

    You may even have a property within the DBConnection class that references
    the ConnectionPool object(s) that it belongs to for easy access from
    DBConnection.

    I don't know what your final goal is, but I thought that's where you are
    headed.

    --
    Business Web Solutions
    ActiveLink, LLC

    Comment

    • matty

      #3
      Re: Can you share variables across objects created from the same class?

      Nikolai Chuvakhin wrote:
      [color=blue]
      > adavis@addpower .com (Anthony Davis) wrote in message
      > news:<e52b1e55. 0308050856.5a96 de38@posting.go ogle.com>...[color=green]
      >>
      >> Can someone please tell me if there is a way to declare variables
      >> within a class that can be shared among each new object created from
      >> this class?[/color]
      >
      > Not really, unless you want to declare them as global...
      >
      > Cheers,
      > NC[/color]

      You have to wait till PHP 5 for static/class variables
      --
      Matt Mitchell - AskMeNoQuestion s
      Dynamic Website Development and Marketing

      Comment

      • Anthony Davis

        #4
        Re: Can you share variables across objects created from the same class?

        matty <matt+nntp@askm enoquestions.co .uk> wrote in message news:<K3XXa.544 54$xd5.3348908@ stones.force9.n et>...[color=blue]
        > Nikolai Chuvakhin wrote:
        >[color=green]
        > > adavis@addpower .com (Anthony Davis) wrote in message
        > > news:<e52b1e55. 0308050856.5a96 de38@posting.go ogle.com>...[color=darkred]
        > >>
        > >> Can someone please tell me if there is a way to declare variables
        > >> within a class that can be shared among each new object created from
        > >> this class?[/color]
        > >
        > > Not really, unless you want to declare them as global...
        > >
        > > Cheers,
        > > NC[/color]
        >
        > You have to wait till PHP 5 for static/class variables[/color]

        Does anyone know when PHP 5 is scheduled for release?
        thanks

        Comment

        Working...