Jump to content

Archived

This topic is now archived and is closed to further replies.

lucerias

PHP syntax question

Recommended Posts

May i know what is the meaning of the following code and what is the -> sign about? Sorry for asking this kind of question, i am new to php world. I have read through some web resource but still fail to obtain the concept from the example provided. Thank you.

$log->info("This page is $page"); 

Share this post


Link to post
Share on other sites
I'm not entirely sure on this, but I think -> is a pointer used to reference a function/variable to class (object).

It makes sense having a read over [url=http://au.php.net/class]this[/url]. The sign *may* also be used for others things, not entirely sure as I'm quite new to php.

Share this post


Link to post
Share on other sites
yes that would be a class - the rest of the code would be something like:

class LogClass
{
    var $blah; // a couple if variables in the class

    function info($str) // a class function
    {
        echo $str;
    }
}

$page = "blah.php" // or wherever the variable for page is coming from

$log = new LogClass;  // assign $log as a log class

$log->info("this page is $page"); // call the class function

the  [color=red]->[/color]  thingy is for the properties and functions within a class, so

$log->blah (without the $) would let you change the value of $blah within the class...


*note: classes are very useful, have a play with them!!

Cheers,
Michael

Share this post


Link to post
Share on other sites
How to echo the $blah parameter in a class? I have problem with the code below.

class LogClass
{
    var $blah; // a couple of variables in the class
    $blah="string";
   
    function info($str) // a class function
    {
        echo $str;
    }
}

$page = "blah.php"; // or wherever the variable for page is coming from

$log = new LogClass;  // assign $log as a log class

$log->info("this page is $page"); // call the class function

echo ($log->$blah) ;

?>

Share this post


Link to post
Share on other sites
Hi

when referring to properties of a class, you do not use the [color=blue]$[/color] sign for the variable.

so it would be

[code]$log->blah = "string";
echo $log->blah;[/code]

also, for the record if you are changing a value of a variable within a class (say in the "info" function) you use [color=blue]$this[/color]

[code]    function info($str) // a class function
    {
        $this->blah = "string";
        echo $str;
    }[/code]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.