Jump to content

Archived

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

mtylerb

Help with ->

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.

Share this post


Link to post
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]

Share this post


Link to post
Share on other sites
LOL.  My friend, you are a genius!  I feel like a royal doofus now.  How did I miss the lack of echo statements.

*Sign*  It was fun.  Got any recommended websites to help with that OOP stuff?

Share this post


Link to post
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 ... :) )

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.