tags:

views:

277

answers:

5

I've seen this "-> " elsewhere used in php. One of the books I used to learn PHP has this in it, but it is never explained. What does it do, how does it work!

The redirect bit I know, but what is happening with the $html variable and the redirect function?

Thanks in advance!

+20  A: 

Note: If you have no idea what an 'Object' is, the next paragraph might not make sense. I added links at the end to learn more about 'objects' and what they are

This will access the method inside the class that has been assigned to HTML.

class html
{
    function redirect($url)
    {
         // Do stuff
    }
    function foo()
    {
       echo "bar";
    }
}
$html = new html;
$html->redirect("URL");

When you create a class and assign it to a variable, you use the '->' operator to access methods of that class. Methods are simply functions inside of a class.

Basically, 'html' is a type of object. You can create new objects in any variable, and then later use that variable to access things inside the object. Every time you assign the HTML class to a varaible like this:

$html = new html;

You can access any function inside of it like this

$html->redirect();
$html->foo(); // echos "bar"

To learn more you are going to want to find articles about Object Oriented Programming in PHP

First try the PHP Manual:
http://us2.php.net/manual/en/language.oop.php
http://us2.php.net/oop

More StackOverflow Knowledge:
http://stackoverflow.com/questions/1224789/php-classes-when-to-use-vs/
http://stackoverflow.com/questions/tagged/oop
http://stackoverflow.com/questions/249835/book-recommendation-for-learning-good-php-oop
http://stackoverflow.com/questions/716412/why-use-php-oop-over-basic-functions-and-when
http://stackoverflow.com/questions/135535/what-are-the-benefits-of-oo-programming-will-it-help-me-write-better-code

Chacha102
This is a real facepalm moment. The sad part is that I had my reputation cap before posting this answer.
Chacha102
Still, +1 ; and as the 10th upvote for this answer, this should get you a (second one ?) "nice answer badge" ;-)
Pascal MARTIN
At least there is that.
Chacha102
I was getting tired of seeing '12' next to the badge count. At least now I get to see a new number for awhile.
Chacha102
lol ^^ Well, beware of what you say about people with 12 bronze badges :-p (And it's not the size that matter ;-) )
Pascal MARTIN
+1  A: 

$html is an object. The redirect function is a method that belongs to this object. I strongly suggest that you read the PHP documentation on classes and objects to explain these concepts.

Rob Knight
+3  A: 

Hi,

In addition to what Chacha102 said (which is the explanation for the particular case in the question you are asking), you really might want to takle a look at the PHP Manual, and its Classes and Objects (PHP 5)

It will teach you many useful things :-)

For instance, you question most certainly has it's answer in the chapter The Basics ;-)

Pascal MARTIN
A: 

$html in your case is not a variable but a class. Just google for 'PHP class tutorial'. redirect in this is case is a member function, which should probably contain similar code:

class html {
    function redirect($url) {
         echo '<META HTTP-EQUIV="refresh" CONTENT="0;URL='.$url.'">';
         exit;
    }
}

This will allow to construct a class from your PHP script like this:

$html = new html;

And you will be able to call it's member:

$html->redirect("www.stackoverflow.com");
Andrejs Cainikovs
A: 

$html is the variable, html is the class.

$html = new html;

puts a new object with class html in the variable $html. Otherwise, that's correct.

Rusky