On Tue, April 11, 2006 6:40 am, Merlin wrote:
>>> The following code:
>>>      class search_helper extends AjaxACApplication
>>>      {
>>>         var $db_username;
>>>         $this->db_username      = $DB_LOGIN;

A 'var' is a 'property' of a Class instance.

It needs to be inside the class, but not inside a function.

$this->db_username = $DB_LOGIN, however, is a statement which must be
inside a function, or used in your script.

>> If you want to set a default:
>>
>> var $db_username = 'xxxxxxxx';

This is also very nifty.

> That looks like to much for just assigning value to a variable?!

If you don't NEED the functions, you can just assign the values any
time you want willy-nilly:

<?php
  class foo(){ };
  $foo = new foo();
  $foo->db_username = $DB_LOGIN;
?>

You don't even NEED to declare the member variable db_username.

I'd recommend AT LEAST doing:
<?php
  class foo(){
    var db_username;
  }

  $foo = new foo();
  $foo->db_username = $DB_LOGIN;
?>

Because that at least clarifies up-front what member variables you
will be using.

You might even document them, if they're not 100% obvious.

> 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();

You cannot just call this function inside a class.

In fact, really, NOTHING gets "called" directly inside a class
definition.

A class definition is all about defining how the object will behave.

Creating the object, and calling the function[s] to make it actually
*DO* something happen outside the class definition.

Of course, methods in the class definition call other methods and
functions and alter things -- but it's like setting up a giant
dominoes series, and knocking over that first dominoe.

The class {} statement sets up all the dominoes.
$foo = new foo();
$foo->start();
hits the first dominoe.

> Is there not something more easy than that. For example $var1 = $var2;

Easier to start with yes.

Easier to understand later, and document, and know what's going on,
not so much...

Also, having the functions to read/write your variables CAN be crucial
later if you decide to change internals of how that data is stored.

Or if you decide that you want to profile how often that data changes,
or how long it takes to change it.

The function provides a single choke-point to be able to moderate your
data traffic.

Not having the function is "easier" but can be trouble-some later when
you want to do something "different" or "more complex" with the data.

If you *KNOW* that for all time into enternity long after you are dead
and gone nobody will ever ever need to do that kind of complex stuff,
then you can go right ahead and just set the variable in your script
anywhere/anytime you want.

Gotta wonder why you'd use OOP under those circumstances in the first
place, but there it is.

-- 
Like Music?
http://l-i-e.com/artists.htm

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to