JUser — Extended user registration
News Joomla Components arrow Tutorials arrow Development tips and tricks arrow Best development practice
Best development practice
Thursday, 18 January 2007

Extending core classes

I have found it absolutely useful to extend any API class you ever use. For example JFile, JText, JRequest, JWatever. I usually create special file that I load in second line in admin.mycomponent.com. Why? Because later you probably will need to do some extra operations.

And yet you do not need to declare every function you use. Just extend the class. My file looks like this:

  1. <?php
  2. class myFolder extends JFolder{}
  3. class myRequest extends JRequest{}
  4. class myError extends JError{}
  5. // and for example
  6. class myFile extends JFile {
  7. function upload($src, $dest)
  8. {
  9. // and here you can add the code
  10. return parent::upload($src, $dest);
  11. }
  12. }
  13. ?>
 

The above example shows class that extends JFile core joomla class. See, now you can set default upload directory or check for maximum upload size that can be part of component public configuration. You can process file to check for viruses, create thumbnail if it is image or whatever.

May be that is not best example for area of application but this is best practice for sure. See also how it is greatly applicable for automatic creating of language file during development or for example this.

  1. <?php
  2. class myError extends JError{
  3. function raiseWarning($num, $text)
  4. {
  5. // Log Error
  6. error_log($text, 3, "error.log");
  7. //alert admin
  8. $sub = "Important Error";
  9. // extend text. More vlues can be added like user ID id
  10. // loged in, request url or any other that can help.
  11. $text .= "User IP: ".$_SERVER['REMOTE_ADDR']; // and more...
  12. $hdr = "From: Robot <robot @ sire . com>"; // and more...
  13. if($num > 1000) mail("admin @ site . com", $sub, $text, $hdr);
  14. return parent::raiseWarning($num, $text);
  15. }
  16. }
  17. ?>
 

Then to alert admin on errors just num your error more the 1000.
Sure you like it :)

More later here

Comments (0)Add Comment

Write comment
quote
bold
italicize
underline
strike
url
image
quote
quote
Smiley
Smiley
Smiley
Smiley
Smiley
Smiley
Smiley
Smiley
Smiley
Smiley
Smiley
Smiley

busy
Last Updated ( Friday, 16 March 2007 )
 
< Prev
Address: Joomla Equipment, Box 1951, Bishkek, 720000, Kyrgyzstan
Telephones: Office: +996 312 68-91-34, Mobile: +996 543 91-81-42
More contacts...