On 10/1/07, Merlin <[EMAIL PROTECTED]> wrote:
>
> Hello everybody,
>
> thank you for all your answers. I am sorry but I just realized that
> there is an huge difference between PHP5 and PHP4 considering OOP.
> Unfortunatelly I am still running PHP4.x and cant change this over
> night. However I still need to implement this class which does work very
> well, just needs handwritten parameters again.
>
> There is one file where the DB login data is stored and the new ajax
> class I am using needs this data as well. Right now I am changin this in
> both files, as I could not figure out how to access the variable from
> inside the class. The code Martin has written seems only to work in PHP5
> as I do get errors.
>
> For now all I do want is to access an outside variable from inside a
> class with PHP4 code. Thats all. I will look into classes more deaply in
> the next days and in PHP5, but it would be very helpful if somebody
> could help me now with getting that variable.
>
> Thank you for your help.
>
> Best regards,
>
> Merlin
>


Merlin,

in php4 there are no restrictions on variable access from outside the class.
you can access any member variable from an instance of the class directly,
here is an example:

class SomeClass {
    var $someVar = 'hello';
}

$classInstance = new SomeClass();

echo $classInstance->someVar;   /// prints hello

i recommend you not do this however, since standard practice is to mark
variables
private and expose access to them through public methods.  as you indicated
php4
does not have ppp support.  but, if you want a tip on how to write decent oo
code
in php4 here it is; drive all access to member variables through methods.
when you
later (if ever) port the code to php5, the member variables can be marked
private and
the member functions can be marked public.  here is a revision of the above
example
to demonstrate:

class SomeClass {
    var $someVar = 'hello';

    /// getter method; return the value of the instance variable
    function getSomeVar() {
        return $this->someVar;
    }

    /// setter method, change the value of the instance variable
    function setSomeVar($someVar) {
       // some validation perhaps
        $this->someVar = $someVar;
    }
}

$someClassInstance = new SomeClass();
echo $someClassInstance->getSomeVar ();   /// prints hello

$someClassInstance->setSomeVar('wow');
echo $someClassInstance->getSomeVar();   /// prints wow

that is good practice per php4 oop programming (imho).  if it doesnt
run you may have to weed out a typo or 2, i just wrote it right into the
email.
also, you dont have to pass data into the constructor only.  typically, you
only pass data into a constructor that a constructor may need to create
an initial state for the object instance.  if an object instance is valid
w/o
some data youre thinking about passing into the constructor, it may be
better
to just have a separate setter method.

-nathan

Reply via email to