Jump to content


Photo

PHP syntax question


  • Please log in to reply
4 replies to this topic

#1 lucerias

lucerias
  • Members
  • PipPipPip
  • Advanced Member
  • 101 posts

Posted 03 October 2006 - 08:29 AM

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"); 

#2 FrOzeN

FrOzeN
  • Members
  • PipPipPip
  • Advanced Member
  • 70 posts

Posted 03 October 2006 - 08:47 AM

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 this. The sign *may* also be used for others things, not entirely sure as I'm quite new to php.

#3 thedarkwinter

thedarkwinter
  • Members
  • PipPipPip
  • Advanced Member
  • 191 posts
  • LocationLondon

Posted 03 October 2006 - 10:12 AM

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  ->  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
Remember - if you don't figure it out yourself, you'll probably forget it tomorrow :)

#4 lucerias

lucerias
  • Members
  • PipPipPip
  • Advanced Member
  • 101 posts

Posted 04 October 2006 - 01:17 AM

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) ;

?>

#5 thedarkwinter

thedarkwinter
  • Members
  • PipPipPip
  • Advanced Member
  • 191 posts
  • LocationLondon

Posted 04 October 2006 - 08:27 AM

Hi

when referring to properties of a class, you do not use the $ sign for the variable.

so it would be

$log->blah = "string";
echo $log->blah;

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

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

Remember - if you don't figure it out yourself, you'll probably forget it tomorrow :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users