Home Tutorials Accessing properties II

Accessing properties II

  previous next
February 11, 2009 by Victor    

Now, that we warmed up, let’s play with the JURI object that is also provided through the JFactory class. I am not changing much, see what you can get out of it.

<?php
  defined('_JEXEC') or die('Restricted access');
 
  $uri =& JFactory::getURI();
  print "\n Current URI Request\n";
  foreach ($uri as $key => $value)
  {
     if (is_string($value))
     {
        print "<p> uri property $key is $value </p>\n";
     }
     else 
     {
        print "<p> uri property $key is not a string </p>\n";
     }
  }
?>

OK, we can print properties but, before it gets very boring let’s use the already provided getter methods to access data, I think you will be more happier. This time we will see the JDate class that is documented here. Calling the JData methods should not puzzle you a lot.

<?php
  defined('_JEXEC') or die('Restricted access');
 
  $date =& JFactory::getDate();
 
  # call to the toMySQL method
  $datemysql = $date->toMySQL();
  print "\n<p> The date as appears in the database is $datemysql </p>\n";
 
  # call to the toUNIX method
  $dateunix = $date->toUNIX();
  print "\n<p> The current date in UNIX format is $dateunix </p>\n";
 
  # call to the fancy toFormat method passing a format string
  $datestr = $date->toFormat ('%H:%M:%S - %d %b %Y');
  print "\n<p> The Current Date is $datestr </p>\n";
 
  # direct call to the _monthToString method, dot just concatenates
  print "\n<p>" . JDate::_monthToString(4) . " or " . 
                               $date->_monthToString(4) . "</p>\n";
?>

As you can see there is a good variety of getter methods, I hope you are familiar with the unix date format, if not check the man page (google for “man date”) and you will find the various options the format string supports. Recall that for invoking a class method directly without referring to an object, you can you use the “::” construct.

Add comment


Security code
Refresh