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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



If you are using PHP4, you could always

class foo {
  var $var1;
  var $var2;
  var $var3;

 function bar(){
  echo $this->var1."<br />";
  echo $this->var2."<br />";
  echo $this->var3."<br />";
 }
}

then in your code

$foo = new foo();

$foo->var1 = 'blah';
$foo->var2 = 'blah';
$foo->var3 = 'blah';

$foo->bar();

Or you could

class foo {
  var $var1;
  var $var2;
  var $var3;

 function foo($var1='',$var2='',$var3=''){
   $this->var1 = $var1;
   $this->var2 = $var2;
   $this->var3 = $var3;
 }

function bar(){
  echo $var1."<br />";
  echo $var2."<br />";
  echo $var3."<br />";
 }
}

$foo = new foo('blah1','blah2','blah3');
$foo->bar();

Note this will have to be written differently in PHP5 and up using public as
var  has been depreciated and using __construct instead of class name as the
constructor.  The  correct  way to set variables inside a class from user
space code was mentioned before using a set method as it allows the set
function to perform additional checks like type and so on and control is
kept to the class.  This can be tedious as it does add a little more code
but will save you in the long run when incase some developer decides to just
over write one of your variables by accident or not can be difficult to
track down.

HTH
Jarratt










On 4/11/06, Merlin <news.groups@xxxxxx> wrote:
>
> chris smith schrieb:
> > On 4/11/06, Merlin <news.groups@xxxxxx> wrote:
> >> chris smith schrieb:
> >>> On 4/11/06, Merlin <news.groups@xxxxxx> 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
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux