Monday 2 June 2008

Passing variables around a class

I'm expecting a fair few of my following blogs to be about using Classes.
It's one of the tools of PHP that I have not really used. Functions yes but
not Classes, I have however hve been trying to use other peoples classes.
This I feel is bad pracise without fully understanding how and why classes
should be used.
I have been using the class activecalender which can be found at
www.phpclasses.org for a booking script. The changes I have been making to
class have been hacks to the code itself. After some studying into classses
it seems this is not the best practise but what i should do is make an
extended class, known as a child class.

'class myclass extends myparent'

Child classes then inherent member functions and member variables from its
parent. I will be coming back to this topic in a future blog, firstly I will
be looking at repackaging some of my own code into classes.
My original problem that has created this tipping point was that I could not
pass variables around the class . The solution is very simple.
in the Call up to the class set the value of the variable you wish to pass
around the class

$cal->item =$item;

from inside the class set up variable

var = $item;

and form inside the function

$this->item

it's that easy!!!

No comments: