PHP Operator

  • Kurthead+1
  • Graduate
  • Graduate
  • Kurthead+1
  • Posts: 130

Post 3+ Months Ago

I'm trying to discern what the role of "->" is in this code. My indecisive interpretation is that "->" pushes the variable "$a", which holds the parameter of the of the Exception into the "getMessage()" parameter. The tutorial doesn't elaborate.

Also, I'm curious how the syntax of the catch parameter "(Exception $a)" works. How the variable is just placed next to "Exception" rather than divided as in other parameters. I just haven't seen anything coded like that in the earlier tutorial chapters, so it undermines my understanding of the rules. Basically I'm asking "What rule does this syntax follow?" Or what are the guild-lines for this type of syntax. Thanks in advance. All of my question pertains to lines 17-19.

(Also, sorry in advance, I explained my query to the best of my abilities and it still seems a little muddy)

PHP Code: [ Select ]
<?php
function checkNum($number)
   {
   if($number>1)
      {
      throw new Exception("Value must be 1 or below");
      }
   return true;
   }
 
try
   {
   checkNum(2);
   echo "If you see this, the number is too low";
   }
 
catch (Exception $a)
   {
   echo "Message: ".$a->getMessage();
   }
?>
 
  1. <?php
  2. function checkNum($number)
  3.    {
  4.    if($number>1)
  5.       {
  6.       throw new Exception("Value must be 1 or below");
  7.       }
  8.    return true;
  9.    }
  10.  
  11. try
  12.    {
  13.    checkNum(2);
  14.    echo "If you see this, the number is too low";
  15.    }
  16.  
  17. catch (Exception $a)
  18.    {
  19.    echo "Message: ".$a->getMessage();
  20.    }
  21. ?>
  22.  
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • ScottG
  • Proficient
  • Proficient
  • ScottG
  • Posts: 447

Post 3+ Months Ago

the -> arrow operator is used to get at the variables and functions that belong to an object.

A quick example would be

some php class (class.php)
PHP Code: [ Select ]
<?php
 
class SomeClass {
   
   public $on_message = array("jack" => 0, "jill" => 0);
   private $jacks_messages = array('Hello', 'How are you?', 'Hey jill you want to run up the hill with me?', 'Ahhh! I tripped.');
   private $jills_messages = array('Hi', "I'm fine and you?", 'Sure I need to goto the well anyway', 'AAAAhhh ha ha ha ha.', 'Umm I think you may need to see a doctor, You sound like a broken record.');
   
   public function get_message($for) {
     
      if(strtolower($for) == 'jack') {
         
         $message = $this->jacks_messages[$this->on_message['jack']];
         $this->on_message['jack']++;
     
      } else {
         
         $message = $this->jills_messages[$this->on_message['jill']];
         $this->on_message['jill']++;
         
      }
     
      return '<span style="color:#' . ((strtolower($for) == 'jack')? 'ff0000;' : '0000ff;') . '">' . ucwords($for) . ' </span>' . $message . '<br />';
     
   }
   
}
 
?>
 
  1. <?php
  2.  
  3. class SomeClass {
  4.    
  5.    public $on_message = array("jack" => 0, "jill" => 0);
  6.    private $jacks_messages = array('Hello', 'How are you?', 'Hey jill you want to run up the hill with me?', 'Ahhh! I tripped.');
  7.    private $jills_messages = array('Hi', "I'm fine and you?", 'Sure I need to goto the well anyway', 'AAAAhhh ha ha ha ha.', 'Umm I think you may need to see a doctor, You sound like a broken record.');
  8.    
  9.    public function get_message($for) {
  10.      
  11.       if(strtolower($for) == 'jack') {
  12.          
  13.          $message = $this->jacks_messages[$this->on_message['jack']];
  14.          $this->on_message['jack']++;
  15.      
  16.       } else {
  17.          
  18.          $message = $this->jills_messages[$this->on_message['jill']];
  19.          $this->on_message['jill']++;
  20.          
  21.       }
  22.      
  23.       return '<span style="color:#' . ((strtolower($for) == 'jack')? 'ff0000;' : '0000ff;') . '">' . ucwords($for) . ' </span>' . $message . '<br />';
  24.      
  25.    }
  26.    
  27. }
  28.  
  29. ?>
  30.  


index page (index.php)
PHP Code: [ Select ]
<?php
 
require_once('class.php');
$class = new SomeClass();
 
?>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
 
<body>
<?php
 
echo $class->get_message('jack');
echo $class->get_message('jill');
echo $class->get_message('jack');
echo $class->get_message('jill');
echo $class->get_message('jack');
echo $class->get_message('jill');
echo $class->get_message('jack');
echo $class->get_message('jill');
 
// Reset jacks messages back abit
$class->on_message['jack']--;
 
for($i=0; $i<5; $i++) {
   
   // Reset jacks messages back abit
   $class->on_message['jack'] --;
   echo $class->get_message('jack');
   
}
 
echo $class->get_message('jill');
 
?>
</body>
</html>
 
  1. <?php
  2.  
  3. require_once('class.php');
  4. $class = new SomeClass();
  5.  
  6. ?>
  7.  
  8. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  9. <html xmlns="http://www.w3.org/1999/xhtml">
  10. <head>
  11. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  12. <title>Untitled Document</title>
  13. </head>
  14.  
  15. <body>
  16. <?php
  17.  
  18. echo $class->get_message('jack');
  19. echo $class->get_message('jill');
  20. echo $class->get_message('jack');
  21. echo $class->get_message('jill');
  22. echo $class->get_message('jack');
  23. echo $class->get_message('jill');
  24. echo $class->get_message('jack');
  25. echo $class->get_message('jill');
  26.  
  27. // Reset jacks messages back abit
  28. $class->on_message['jack']--;
  29.  
  30. for($i=0; $i<5; $i++) {
  31.    
  32.    // Reset jacks messages back abit
  33.    $class->on_message['jack'] --;
  34.    echo $class->get_message('jack');
  35.    
  36. }
  37.  
  38. echo $class->get_message('jill');
  39.  
  40. ?>
  41. </body>
  42. </html>
  43.  

Post Information

  • Total Posts in this topic: 2 posts
  • Users browsing this forum: No registered users and 123 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
cron
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.