Re: how to assign a value to a variable inside a class

From: Date: Tue, 11 Apr 2006 11:57:03 +0000
Subject: Re: how to assign a value to a variable inside a class
References: 1 2 3 4 5 6 7  Groups: php.general 
Request: Send a blank email to php-general+get-233754@lists.php.net to get a copy of this message
Merlin wrote:
chris smith schrieb:
On 4/11/06, Merlin <news.groups@web.de> wrote:
chris smith schrieb:
On 4/11/06, Merlin <news.groups@web.de> wrote:
Hi there, no much simpler. I do not need to assign the value from outside the class. This is just inside the class. I have the login data for a database saved in a file and would like to use simply the variable $DB_login inside this class. This does not work: $this->db_username = $DB_login;
That's the right way to do it. What are you seeing? -- Postgresql & php tutorials http://www.designmagick.com/
The following code:
     class search_helper extends AjaxACApplication
     {
        var $db_username;
        $this->db_username      = $DB_LOGIN;
Try it like this: class search_helper extends AjaxACApplication { var $db_username; ... function SetDBUsername($db_username) { $this->db_username = $db_username; } If you want to set a default: var $db_username = 'xxxxxxxx'; -- Postgresql & php tutorials http://www.designmagick.com/
That looks like to much for just assigning value to a variable?! I would need 4 of those functions just to set the value of 4 variables to a value saved inside another variable. If I do understand you right, I would also have to call that function inside the class to get that value set?: function SetDBUsername($db_username) { $this->db_username = $db_username; } SetDBUsername(); Is there not something more easy than that. For example $var1 = $var2; Merlin
You're right... This is the wonders of OOP. Some people see this as nice because only your class has direct access to set your variables...so inside your 'set' function you can do validation, formatting, etc... to make sure you are setting the variable to a proper value.... Some people like the control, others think it is too much work with little gain, this is largely a political issue though, so we won't get into it too much. Basically if you want to use classes you have to play by the rules of OOP. To set and get a member variable within a class you need to define a function to do so. -Brad

« previous php.general (#233754) next »