What does this php construct mean: $html->redirect

2020-04-08 14:08发布

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!

标签: php oop
5条回答
Viruses.
2楼-- · 2020-04-08 14:29

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:
PHP Classes: when to use :: vs. ->?
https://stackoverflow.com/questions/tagged/oop
https://stackoverflow.com/questions/249835/book-recommendation-for-learning-good-php-oop
Why use PHP OOP over basic functions and when?
What are the benefits of OO programming? Will it help me write better code?

查看更多
爷、活的狠高调
3楼-- · 2020-04-08 14:29

$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.

查看更多
smile是对你的礼貌
4楼-- · 2020-04-08 14:39

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 ;-)

查看更多
Viruses.
5楼-- · 2020-04-08 14:39

$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.

查看更多
叼着烟拽天下
6楼-- · 2020-04-08 14:40

$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");
查看更多
登录 后发表回答