[thelist] PHP n00b

Brady Mitchell mydarb at gmail.com
Fri Jan 4 11:46:25 CST 2008

On Jan 4, 2008 8:38 AM, Kevin Stevens <kjs at ratking.co.uk> wrote:
> I'm looking for a job in web design and a friend recommended I learn PHP
> because it I'm more likely to find something with that skill than the
> ASP I already know.

Welcome to PHP. :)

> <?php
> class cat
>          {
>           var $age;
>           function cat($now_age) { $this->age = $now_age; }
>           function birthday() { $this->age++; }
>          }
> $fluffy = new cat(1);
> echo "Age is $fluffy->age <br>";
> echo "Birthday<br>";
> $fluffy->birthday();
> echo "Age is $fluffy->age <br>";
> ?>

Have you done object oriented code before? If not you may need to read
up on it a little more to understand what's going on here.

When using classes you have methods and properties. Methods are the
functions that perform some action, and properties are the variables
that hold information about that object.

In this example, the cat has one property ( $age ) and one method (
birthday() ). The cat function is the constructor, it's called
automatically whenever a new cat object is created.

> Could somebody tell me if my understanding of this is correct please.
> The piece of code '$this->age = $now_age' is what is baffling me

> 2. Presumably I cannot use a variable as a parameter of a function

Actually, function parameters are pretty much always variables.
Usually if you're hard coding something you should re-think how you're
solving the problem.

> '$this->age = $now_age' effectively puts the value of the variable into
> the parameter. Is this right?

Yes. In the example you posted, $cat->age is 1 when the cat object is
created. Then after calling $cat->birthday(), $cat->age is 2.

> If I am correct it seems a pretty ar$e-about-face way of doing things,
> the purpose of which will no doubt be revealed to me at some later date

The last part of your statement is true, you'll get it after you do
more object oriented programming. If you've never done OOP before, you
might want to take some of your procedural ASP and convert it to
procedural PHP to see how that works. Then once you have a better
handle on it, make the jump to OOP.



More information about the thelist mailing list