Professional Web Applications Themes

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

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($database) { 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 ...

  1. #1

    Default 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($database) {
    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
    Anthony Davis Guest

  2. #2

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

    Anthony Davis wrote on Tuesday 05 August 2003 09:56:
    > 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.
    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
    [url]www.active-link.com/intranet/[/url]
    Zurab Davitiani Guest

  3. #3

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

    Nikolai Chuvakhin wrote:
    > [email]adavisaddpower.com[/email] (Anthony Davis) wrote in message
    > news:<e52b1e55.0308050856.5a96de38posting.google. com>...
    >>
    >> 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?
    >
    > Not really, unless you want to declare them as global...
    >
    > Cheers,
    > NC
    You have to wait till PHP 5 for static/class variables
    --
    Matt Mitchell - AskMeNoQuestions
    Dynamic Website Development and Marketing
    matty Guest

  4. #4

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

    matty <matt+nntpaskmenoquestions.co.uk> wrote in message news:<K3XXa.54454$xd5.3348908stones.force9.net>.. .
    > Nikolai Chuvakhin wrote:
    >
    > > [email]adavisaddpower.com[/email] (Anthony Davis) wrote in message
    > > news:<e52b1e55.0308050856.5a96de38posting.google. com>...
    > >>
    > >> 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?
    > >
    > > Not really, unless you want to declare them as global...
    > >
    > > Cheers,
    > > NC
    >
    > You have to wait till PHP 5 for static/class variables
    Does anyone know when PHP 5 is scheduled for release?
    thanks
    Anthony Davis Guest

Similar Threads

  1. #25377 [Bgs]: Class variables can be added out of class definition
    By helly@php.net in forum PHP Development
    Replies: 0
    Last Post: November 22nd, 08:24 PM
  2. Replies: 0
    Last Post: November 18th, 08:38 PM
  3. Instance- and class-variables (was mixing in class methods)
    By dblack@superlink.net in forum Ruby
    Replies: 2
    Last Post: October 3rd, 03:52 AM
  4. Instance- and class-variables (was mixing in class methods)
    By dblack@superlink.net in forum Ruby
    Replies: 0
    Last Post: October 2nd, 04:19 PM
  5. Instance- and class-variables (was mixing in class methods)
    By dblack@superlink.net in forum Ruby
    Replies: 0
    Last Post: October 2nd, 12:46 PM

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
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139