Quick simple PHP question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
user681
Forum Newbie
Posts: 4
Joined: Mon Nov 01, 2010 11:40 am

Quick simple PHP question

Post by user681 »

Code: Select all

lass example_class_name
{
   public $attribute;
      function operation ($param) 
       {
        $this -> attribute = $param
       echo this ->attribute;
        }
}


As far as I understands.. class example_class_name creates a new class called example_class_name
But What does public $attribute do???
Would $attribute be a variable in this case?
"operation" is a function we've just created and it has a parameter called $param correct??
I don't really understand $this -> attribute =$param
Isn't $param a parameter of the function operation?
It'd be great if someone could explain to me what each line of this simple php script do
Reviresco
Forum Contributor
Posts: 172
Joined: Tue Feb 19, 2008 4:18 pm
Location: Milwaukee

Re: Quick simple PHP question

Post by Reviresco »

You're declaring $attribute to be a variable.

In the function, you're assigning a value to the variable. The value comes from the parameter that's being passed via the function.

So, say you used the function like this: $a->operation('kittens'), the value of $a->attribute would be 'kittens' and it would echo "kittens" on the page.

One thing though -- in the function, you're missing the semicolon at the end of the first line.
user681
Forum Newbie
Posts: 4
Joined: Mon Nov 01, 2010 11:40 am

Re: Quick simple PHP question

Post by user681 »

Reviresco wrote:You're declaring $attribute to be a variable.

In the function, you're assigning a value to the variable. The value comes from the parameter that's being passed via the function.

So, say you used the function like this: $a->operation('kittens'), the value of $a->attribute would be 'kittens' and it would echo "kittens" on the page.

One thing though -- in the function, you're missing the semicolon at the end of the first line.
Hey thanks for the quick response
You've cleared/comfirmed things up for me
I have one last question...
is passing down your value through parameter a practical?? if so could you give an example of how it is used in a real life situation
It just seems odd to me to use parameter to pass down a value..
Reviresco
Forum Contributor
Posts: 172
Joined: Tue Feb 19, 2008 4:18 pm
Location: Milwaukee

Re: Quick simple PHP question

Post by Reviresco »

Code: Select all


function display_recipe($recipe_type) {
   if($recipe_type == 'vegan') {
       return 'carrot cake';
   }
   else if($recipe_type == 'vegetarian') {
       return 'cheese dip';
   }
   else {
      return 'barbecued ribs';
   }
}

user681
Forum Newbie
Posts: 4
Joined: Mon Nov 01, 2010 11:40 am

Re: Quick simple PHP question

Post by user681 »

Thanks again for the swift response..

Are there any books that you suggest for learning PHP and OOP in PHP?
Reviresco
Forum Contributor
Posts: 172
Joined: Tue Feb 19, 2008 4:18 pm
Location: Milwaukee

Re: Quick simple PHP question

Post by Reviresco »

I really like this book, PHP 5 in Practice:

http://www.amazon.com/PHP-5-Practice-El ... 0672328887

It's not exactly a beginner book, but it does explain a lot of things really clearly.

But this is the book I learned it all from - may be a little out of date now (2003):

http://www.amazon.com/dp/067232525X?tag ... BBPQYYY4M&
user681
Forum Newbie
Posts: 4
Joined: Mon Nov 01, 2010 11:40 am

Re: Quick simple PHP question

Post by user681 »

Reviresco wrote:I really like this book, PHP 5 in Practice:

http://www.amazon.com/PHP-5-Practice-El ... 0672328887

It's not exactly a beginner book, but it does explain a lot of things really clearly.

But this is the book I learned it all from - may be a little out of date now (2003):

http://www.amazon.com/dp/067232525X?tag ... BBPQYYY4M&
lol PHP and MYSQL Web Development Edition 4 is what I'm reading!! and the example above was taken from the book cuz I didn't understand it much..particularly the section about OOP is confusing

anyway thanks again
Post Reply