Jump to content

PHP syntax question


lucerias

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"); 
Link to comment
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.
Link to comment
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
Link to comment
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) ;

?>
Link to comment
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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.