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
https://forums.phpfreaks.com/topic/22837-php-syntax-question/
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
https://forums.phpfreaks.com/topic/22837-php-syntax-question/#findComment-102934
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
https://forums.phpfreaks.com/topic/22837-php-syntax-question/#findComment-103489
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
https://forums.phpfreaks.com/topic/22837-php-syntax-question/#findComment-103594
Share on other sites

Archived

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

×
×
  • 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.