Navigation:  "The Jet Engine" PHP Application Framework > Programming Concepts > Jet Engine Objects >

Jet Object: Core Methods

Previous pageReturn to chapter overviewNext page

Methods available to your application or plugin from the jet core library.

 

Note that all methods are available through the global object variable $jet

 

 

Define the "JET" class.

 

Usage:  $jet = new JET();

 

Thereafter, you can call these functions using simple OOP Statements, such

as $jet->init();  or list($y,$m,$d) = $jet->datesplit($mydatestring);

 

 

 

 

 

encrypt($string) and decrypt($string)

 

USAGE:    $mykey = "sdlkfjdfsljflkjsdf";

         $string_to_encrypt = "somethingverysecret";

       

         $encrypted = $jet->encrypt( $string_to_encrypt, $mykey );

         $decrypt = $jet->encrypt( $encrypted, $mykey );

 

 

NOTES:    Calling this function creates some very useful class variables

         for use globally in your application.  Some of these may seem

         redundant, given that PHP already has global variables for all

         of these.  However, when you're writing applications that you

         intend to distribute, you never know what version of PHP is

         on your customers server, and how they've set it up.

         e.g.Do you use $_GET, $_SERVER['GET'], or $HTTP_GET_VARS?

 

 

 

 

 

 

       

 

generate_password ($length )

 

USAGE:    $password = $jet->generate_password(10);

 

NOTES:    Generates a completely random password having "$length" number

         of characters.

 

 

     

 

 

       

 

Cache Control using "memcache"

 

This is a simple system of saving variables into static memory on the server

using PHP's "Memcache" Module (http://us.php.net/manual/en/book.memcache.php)

 

For applications that read a large set of config options on every page load

or perhaps retrieve the same set of data from a database on every page load

using Memcache will greatly speed up your program's performance as well as

reduce the server load.  It should only be used for storing values that very

rarely change.

 

We highly recommend that you flush the memcache regularly.

 

memcache_init ($port )

 

USAGE:    $jet->memcache_init($port);

 

NOTES:    Connects to a "memcache" server at the port number specified

 

         If you plan on using memcache, you must call this function at

         the start of your program in order to connect to the memcache

         server.  THen and only then will the read/write/delete functions

         that allow you access to the memory cache work.

 

         Requires the "XSLT" library to be compiled into your PHP

       

 

write_cache( $key, $value )

 

USAGE:    $jet->write_cache($key,$value);

 

NOTES:    Write a key/value pair to the memory cache.  This can be a simple

         variable assignment, or as complex as an entire associative

         array.

 

         e.g.

         $jet->write_cache( "myname", "Earl" )

 and later ... $myname = $jet->read_cache("myname");

 

         or ...

 

         $settings['favorite_color'] = "blue";

         $settings['favorite_food'] = "pudding";

         $settings['favorite_person'] = "myself";

 

         $jet->write_cache( "settings", $settings );

 and later ... $settings = $jet->read_cache("settings");

 

 

read_cache( $key )

 

USAGE:    $variable = $jet->read_cache($key);

 

NOTES:    Reads a value from the memory cache that was previously set

 

         e.g.

         $jet->write_cache( "myname", "Earl" )

 and later ... $myname = $jet->read_cache("myname");

 

         or ...

 

         $settings['favorite_color'] = "blue";

         $settings['favorite_food'] = "pudding";

         $settings['favorite_person'] = "myself";

 

         $jet->write_cache( "settings", $settings );

 and later ... $settings = $jet->read_cache("settings");

 

 

delete_cache ($key)

 

USAGE:    $variable = $jet->delete_cache($key);

 

NOTES:    Deletes a value from the memory cache that was previously set

 

         e.g.

         $jet->delete_cache( "myname" )

 

 

nuke_cache()

 

USAGE:    $variable = $jet->nuke_cache();

 

NOTES:    This will empty out your entire memory cache

 

 

     

 

   

       

 

cleanup_string( $string )

 

USAGE:    $string = "My Dog Has Fleas ../../etc/passwd"

         $cleaned = $jet->cleanup_string( $string, "_", 1);

         // Returns: "My Dog Has Fleas ______etc_passwd"

 

NOTES:    Sanitizes a string.

         Takes 3 parameters

 1: The string to be sanitized (required)

 2: A character to replace invalid charactesr with (optional)

 3: Preserve the original case of the string (optional, but defaults to "no")

 

 

     

 

 

 

       

 

xml_translate( $xml, $xsl )

 

USAGE:    $xml = implode( "", file("/path/to/somefile.xml") );

         $xsl = implode( "", file("/path/to/mystylesheet.xsl") );

         $xhtml = $jet->xml_translate( $xml, $xsl );

         exho $xhtml;

         exit;

 

NOTES:    This function turns the combination of XML data and an XSLT Stylesheet

         into something that your browser can handle.

 

         Requires including the "./lib/3rdparty/xslt.inc" library into your app

 

 

 

strictify($string)

 

USAGE:    $string = $jet->strictify($string);

 

NOTES:    Turns any string into one that can live within <xml> tags safely

 

 

 

unstrictify($string)

 

USAGE:    $string = $jet->unstrictify($string);

 

NOTES:    Removes XML Encoding from strings in XML Data

 

 

 

cdata($string)

 

USAGE:    $string = $jet->cdata($string);

 

NOTES:    Encloses a string within xml friendly <![CDATA[]]> tags.

 

 

 

 

datestamp($date)

 

USAGE:    $timestamp = $jet->datestamp( "20051031" );

 

NOTES:    Turns a date, like October 31, 2005 into a unix timestamp.

 

         We use the YYYYMMDD date format in a lot of programming, because

         this is a simple and easy way to store a date in a database and

         make it sortable as well as readable.  This functions, and others

         that we've got here work with dates in that format.

 

 

 

 

datesplit($date)

 

USAGE:    list( $year, $month, $day ) = $jet->datesplit( "20051031" );

         echo "Get some candy on $month/$day/$year";

 

NOTES:    Returns an array containing year, month, day from a YYYYMMDD

         date string.  You could then use those values to format a date

         for visual display  how you like.

 

         We use the YYYYMMDD date format in a lot of programming, because

         this is a simple and easy way to store a date in a database and

         make it sortable as well as readable.  This functions, and others

         that we've got here work with dates in that format.

 

 

 

 

nth($integer)

 

USAGE:    $visual_number = $jet->nth( 123 );

         // Returns 123rd

 

NOTES:    Turns any integer into it's "nth" equivalent

         i.e.  1st, 2nd, 3rd, 4th, etc....

 

     

 

 

       

 

aasort( $associative_array,$rules )

 

USAGE:    $months = array();

         array_push( $months, array( "name" => "January", "weather" => "COLD" ) );

         array_push( $months, array( "name" => "February", "weather" => "COLD" ) );

         array_push( $months, array( "name" => "March", "weather" => "WARM" ) );

         array_push( $months, array( "name" => "April", "weather" => "WARM" ) );

         array_push( $months, array( "name" => "May", "weather" => "WARM" ) );

         array_push( $months, array( "name" => "June", "weather" => "HOT" ) );

         array_push( $months, array( "name" => "July", "weather" => "HOT" ) );

       

         $jet->aasort($months, "+name");

 Sorts the $months array by the month name, from A-Z

 

         $jet->aasort($months, "-name");

 Sorts the $months array by the month name, from Z-A

 

         $jet->aasort($months, "+weather");

 Sorts the $months array by the weather, from A-Z

 

         $jet->aasort($months, "-weather, +name");

 Sorts the $months array by the weather, from Z-A

 and within the weather, sort by name from A-Z

 

 

NOTES:    Sorts a multi-dimensional associative array by any of it's keys, or

         even by a combination of keys.  Use "+" and "-" to indicate sort

         direction, separate multiple sorting keys with commas, as shown in

         the last example above.

 

     

 

valid_url( $url )

 

USAGE:    if ( $jet->valid_url( $jet->CGI['url'] ) ) { echo "OK"; }

         else { echo "Invalid URL"; }

       

 

NOTES:    Checks if a URL is valid, returns either TRUE or FALSE.  In the

         example given, we are checking the validity of a URL that was

         sent in via a form on a web page.

 

     

       

 

valid_email( $email_address )

 

USAGE:    if ( $jet->valid_email( $jet->CGI['email'] ) ) { echo "OK"; }

         else { echo "Invalid Address"; }

       

 

NOTES:    Checks if an email address is valid, returns either TRUE or FALSE.

         In the example given, we are checking the validity of an address

         that was sent in via a form on a web page.

 

     

 

 

dynamic_url( $args, $path_info_format )

 

USAGE:    $values = array();

         $values['name'] = $somevalue;

         $values['address'] = $somevalue1;

         $values['city'] = $somevalue2;

 

         $url = dynamic_url($values);

       

 

NOTES:    Takes an associative array, and returns the URL to the current

         program, with fully url encoded set of variables.   In the

         example above, the program would return this (assuming the

         program name was "test.php", and that you had previously

         done something to create values for the $somevalue variables...

 

         /path/to/test.php?name=Fred+Smith&address=123+Main+St&city=Seattle

 

         The optional second paramater "path_info_format" tells the function

         to create the URL in a search engine friendly manner (only programs

         that use "The Jet" will be able to parse these urls).  In our example,

         the URL returned would be this:

 

         $url = dynamic_url($values,1);

 

         /path/to/test.php/name::Fred+Smith/address::123+Main+St/city::Seattle/

 

 

 

write_file( $file_path, $text )

 

USAGE:    $timestamp = time();

         $filename = "/full/server/path/to/$timestamp.txt";

         $text_to_save = $jet->CGI['blog_entry'];

       

         list( $ok, $error ) = $jet->write_file( $filename, $text_to_save );

         if ( ! $ok ) { die($error); }

       

 

NOTES:    Writes the value you send in the $text variable to the file that

         you specify.  In the example, we are posting a blog article to

         a file with a name that we created based on the current timestamp

         to make it unique.  We fail with an error message if the file

         cannot be written to or created.

 

getmicrotime()

 

USAGE:    $mc = $jet->getmicrotime();

         $time = time();

         echo "Time: $time / Microtime: $mc";

 

         Outputs:

         Time: 1222206943 / Microtime: 1222206943.48

 

NOTES:    If you need more accurate time than the down-to-the-second

         timestamps that the built-in time() function provides, but in

         a relevant number, this function returns the time stamp along

         with hundredths of the second.

 

 

print_r( $variable )

 

USAGE:    $jet->print_r( $variable );

 

NOTES:    Just an HTML friendly "print_r()" ... wraps PHP's native print_r

         inside <pre> tags so you can easily view it in a browser

 

     

 

APPLICATION BENCHMARKING

 

These 3 functions allow you to set internal timers on your code so that you

can find places that are bogging down your execution times, and identify

these problem areas.    Here's a simple example that compares 2 ways of

counting.  In your actual programming, you'll put this into every function

separately and then can really track the performance of your entire program.

 

NOTE:  Unless you send "benchmark=1" as an additional query string parameter

      with your program URL, these functions will not operate.  This is to

      save the overhead of benchmarking when you don't actually care to

      see the results.   When you want to check them, just send that extra

      paramater.

 

 

global $jet;

 

$jet = new JET();

 

count_using_for();

count_using_while();

 

$jet->show_benchmark();

 

exit;

 

function count_using_for() {

 

   global $jet;

 

   $jet->start_benchmark( "count_using_for" );

 

   // Do something complely useless.

 

   for ( $x = 1; $x <= 100000; $x++ ) { $thisvariable = $x; }

 

   echo "I just counted to 100,000!!!!<BR><BR>";

 

   $jet->stop_benchmark( "count_using_for" );

 

}

 

function count_using_while() {

 

   global $jet;

 

   $jet->start_benchmark( "count_using_while" );

 

   // Do something complely useless.

 

   $x = 100000;

   while ( $x >= 1 ) { $x--; }

 

   echo "I just counted to 100,000!!!!<BR><BR>";

 

   $jet->stop_benchmark( "count_using_while" );

 

}

 

This little program outputs something like this:

 

   I just counted to 100,000!!!!<BR><BR>

   I just counted to 100,000!!!!<BR><BR>

 

     <table id='bench' align='center' width='500' border='1' cellpadding='1'>

        <tr style='background:black'>

          <th style='color:white'>Function</th>

          <th style='color:white'>Exec Time</th>

          <th style='color:white'>Running Time</th>

        </tr>

       

      <tr>

         <td width='400'>count_using_for</td>

         <td align='right' style='color:maroon'>0.0499999523163</tc>

         <td align='right' style='color:blue'>0.0499999523163</tc>

      </tr>

 

      <tr>

         <td width='400'>count_using_while</td>

         <td align='right' style='color:maroon'>0.0599999427795</tc>

         <td align='right' style='color:blue'>0.109999895096</tc>

      </tr>

 

        <tr style='background:#efefef'>

          <td colspan='3' align='right'>0.109999895096</td>

        </tr>

     </table>

 

 

 

start_benchmark( $function_name )

 

USAGE:    $jet->start_benchmark( "myfunction" );

         i.e.

 

NOTES:    This function starts the timer to see how long it taks to run

         you function.  In this example, "myfunction".

 

         Put this in as the first line of any function that you wish to

         behchmark, making sure that you specify the function name.

 

 

 

stop_benchmark( $function_name )

 

USAGE:    $jet->stop_benchmark( "myfunction" );

         i.e.

 

NOTES:    This function stops the timer to see how long it taks to run

         you function.  In this example, "myfunction".

 

         Put this in as the last line of any function that you wish to

         behchmark, making sure that you specify the function name.

 

 

show_benchmark()

 

USAGE:    $jet->show_benchmark();

 

NOTES:    This function outputs a formatted report allowing you to see the

         execution times of every function that you have set a timer for,

         to help you find the bottlenecks in your code.

 

     

throw_error()

 

USAGE:    $error_handler = set_error_handler("throw_error");

 

NOTES:    This is a custom error handling function that gives PHP

         Errors in a more readable format

     

 

 

 


This help file was created with an unregistered evaluation copy of Help & Manual. © EC Software. All rights reserved. This message will not appear if you compile this help file with the registered version of Help & Manual.