Page 1 of 1
Quick simple PHP question
Posted: Mon Nov 01, 2010 11:44 am
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
Re: Quick simple PHP question
Posted: Mon Nov 01, 2010 12:07 pm
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.
Re: Quick simple PHP question
Posted: Mon Nov 01, 2010 12:15 pm
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..
Re: Quick simple PHP question
Posted: Mon Nov 01, 2010 12:20 pm
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';
}
}
Re: Quick simple PHP question
Posted: Mon Nov 01, 2010 12:27 pm
by user681
Thanks again for the swift response..
Are there any books that you suggest for learning PHP and OOP in PHP?
Re: Quick simple PHP question
Posted: Mon Nov 01, 2010 2:51 pm
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&
Re: Quick simple PHP question
Posted: Mon Nov 01, 2010 3:39 pm
by user681
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