Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

oop

php

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!

like image 706
Kirrus Avatar asked Nov 27 '22 19:11

Kirrus


1 Answers

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?

like image 155
Tyler Carter Avatar answered Dec 08 '22 22:12

Tyler Carter