Hey moonwitch,
I not sure whether the book stated what you say or not, nor am I going to say they are right or wrong but the problem is $joke exists outside the function, it's not global either, but if the book suggests this as an example, then it's quite possible they work with global which was default on older PHP versions and that these variables have access from anywhere in the program.
How I like using classes is creating functions to set the variables but in this case I'll leave that out and just explain the problems.
First problem is the variable outside the object, outside the function as well.
<?php
class smilie
{
[/tab]var $joke;
[tab]function make_smile()
[/tab]{
[tab] [/tab]echo $this->joke;
[tab]}
}
$funny = new smilie;
$funny->joke = 'hahaha';
$funny->make_smile();
?>
That's how I would do this example (well keeping as close to the original code), everything is inside the Object, we create our new object, have access to the object variable $joke but we must call it through our Object not through it's variable name, I also use the $this variable, which is a variable used to store the current object that called it.
If you wanted to do it your example way, that's also easy enough, but it needs a few adjustments.
<?php
$joke = 'woohoo';
class smilie
{
[tab][/tab]function make_smile($joke)
[/tab]{
[tab][tab][/tab]echo $joke;
[/tab]}
}
$funny = new smilie;
$funny->make_smile($joke);
?>
See we needed to pass the $joke contents with our function, so that it can reside inside, however the creation of function make_smile($joke), the $joke part of this is not the same as the $joke bit we set at the beginning, we could have said function make_smile($joke2) and tried printing $joke and it wouldn't work because $joke does not exist in that function, but if we printed $joke2 out it would work, the $joke we passed in our function becomes make_smile($joke2 = $joke); so now $joke2 holds the contents of what $joke has, but it still doesn't have access to our $joke variable.
Cheers,
MC
p.s. Thought I'll add how I'll actually do this class
<?php
class smilie
{
[tab]var $joke;
[tab][/tab]function addJoke($joke)
[/tab]{
[tab][tab][/tab]$this->joke = $joke;
[/tab]}
[tab]function make_smile()
[/tab]{
[tab] [/tab]echo $this->joke;
[tab]}
}
$funny = new smilie;
$funny->addJoke('hahaha');
$funny->make_smile();
?>
QUOTE(moonwitch @ Apr 5 2005, 07:36 PM)
What gets me is the given explanation, "you use the new statement" what new statement?? LOL I did fine with arrays, that's all cool. What I get from this piece of code is that you're defining a class called "foo" which consists out of a function "do_foo" which only outputs a string "doing foo.". But where in THAT lil piece of code are you creating a new object? The second part.. $bar = new foo; Isn't that wrong code? I mean no single or double quotes? And on last line I am fully lost.
moonwitch, you have to understand, that to use an object you can't use the object directly, you must give the object it's own storage, the object is just a set of instructions, it's not a variable you can call on.
so when you do
$bar = new foo; you are actually storing all the instructions for foo inside of bar, which is pretty much the whole object.
single or double quotes, $bar = "new foo"; is actually a string of characters not what we were intending on doing, which is creating a new object inside our variable $bar;
The last line pretty much uses your new object you created and calls the function do_foo();
Imagine this, but don't take it in, the class foo, foo->do_foo(); foo is the object, foo calling do_foo(); is our object calling the function, now we replaced foo (the object) with our variable $bar, so now $bar is now the new object replacing foo's position, just remember, foo is only a set of instructions for whatever variable wants to take it on, else it's just code that doesn't get used at all.
We could write a lot of code within our program, that never gets used at all, but if we wanted to we could use it, because it exists. Classes are pretty much libraries of code we would create to do specific things, but we would actually distribute it as a library of functions, instead of actually writing it within our code, we would just include it.
So if I had a class that created HTML pages, (this class does exist), I would include that class, create a new object to use this class and then work with that, all I would really need to know, is what functions exist and what to pass within those functions of this class to actually use it, I should not need to know what variables it creates, nor have to worry that the variables used in this class will conflict with my own program variables, since those variables are contained inside the class only. (well not worrying about the variables should be something, because usually we create functions to take care of initialising the variables, not actually doing it ourselves).
e.g. We might have a function that sets the title of our page and it might be a function called setTitle($title) { $this->Title = $title; } and that's it, the function set our titlte for us just by us calling the function to do this $object->setTitle('My Title');, this is how our classes should be done.
MC
Reply