* Thus wrote PHP Email List ([EMAIL PROTECTED]):
> Why is it in a class, so important for you to initialize your $variables?
> 
> In the following example, I have my class started and then took out or
> atleast commented my "var $variable" and the code still does what it should.
> So why is it so important that you initialize these variables this way?
> 
> [:::CODE:::]
> <?php
> class test {
> 
>       //var $z;               <------This is the part I don't see
>                               the relevance in doing as here this is
>                               commented out and the class works as expected.
> ...
> 
> I guess I'm just confused on why you would want to initialize a variable and
> not assign a value to it if you know what it is? Like in my Database class,
> I made my username, pass, localhost, etc variables initiated with their
> values, but kept getting errors from mysql that I didn't have a "Valid"
> mysql resource. I guess after reading this in 4 books and on php.net, they
> all do this but none of them go into WHY it's so important.

Defining a variable in a class using 'var $z', keeps some sort of
structure for the class.  PHP has always kept the creation of
variables simple for the programmer and classes arn't an exception.
You will find, however, that most classes you'll see will have the
variables defined.

PHP5 actuall expands on the declaration of variables, it adds a
few new ways to define variables:

class test {
  /* Accessable to any body */
  public    $public_var;

  /* Only accessable to any instance of 'test' or 
     any class that inherits 'test' via Extends */
  protected $protected_var;

  /* Only accessable to instances of class 'test' */
  private   $private_var;

  /* the static keyword makes this variable shared accoss all
     instances of 'test'.  and is applied to either public,
     protexted or private scope */
  static public  $static_var;

These keywords can also be applied to the declaration of functions
ie:
  public function test_func() { }


Curt
-- 
"I used to think I was indecisive, but now I'm not so sure."

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

Reply via email to