What does this php construct mean: $html->redirect(“URL”)?

前端 未结 5 834
名媛妹妹
名媛妹妹 2021-01-19 18:21

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

5条回答
  •  青春惊慌失措
    2021-01-19 19:12

    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?

提交回复
热议问题