Jump to content

Help with ->


mtylerb

Recommended Posts

I have searched around various search engines but with relatively little luck (search engines won't search for ->).  I'm trying to figure out exactly what the symbols -> do.  I'm just trying to fix a random quote module for Mambo.  I've seen it used in phpBB but I didn't really understand it there either.  So, please excuse my thick skull, the file it's used in reads:

[code]<?php
/**
*
*
**/

defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );

$database->setQuery("SELECT * FROM mos_quotes");
$quotes = $database->loadObjectList();
$count = count($quotes);
if (!$count)
{
echo ("No quotes yet entered.");
}
else
{
$id = rand(1,$count);
$quote = $quotes[$id];

?>
<p>"<?php $quote->quote ?>" -- <?php $quote->author ?></p>
<p>Quote Number <?php $quote->id ?></p>
<?php
}
?>[/code]

Currently though the only output I'm getting is:

[quote]"" --

Quote Number
[/quote]

I would probably be able to figure it out and fix it relatively easy if I could understand what the -> does.  I understand it has something to do with OOP but it's going right over my head.  I could probably just re-write it to my own whims, but I would rather just keep the coding as close to the original as possible, not to mention my own programming would likely be less efficient then the original was intended to be.
Link to comment
Share on other sites

in php it can have a few uses. for the one you mention, you're right - it involves OOP. to keep it simple, take the class:
[code]
<?php
class simpleclass
{
   function add($a, $b)
   {
      $result = $a + $b;

      return $result;
   }
}
?>
[/code]

using the -> is a way of referencing a method (function) within a class. so:
[code]
$test = new simpleclass();
echo $test->add(5,10); // returns 15
[/code]

from your code, all i can see is that you  maybe missing a few 'echo' statements. so:

[code]
<p>"<?php echo $quote->quote ?>" -- <?php echo $quote->author ?></p>
<p>Quote Number <?php echo $quote->id ?></p>
[/code]
Link to comment
Share on other sites

http://www.devarticles.com/c/a/PHP/Object-Oriented-Programming-in-PHP/ is a good start to get the basics down. Further from that, the best way to learn is to get your hands dirty and look at OOP in action. I personally learnt by downloading a copy of phpBB (and other PHP packages) and looking at the code.

the good thing about OOP is you can keep it as basic or make it as complex as you like. for example, the 'simpleclass' i put down a couple of posts up is a fully working class (albeit not very useful). keep it simple and move from there, as it can get messy (as i'm realising now ... :) )
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.