Support Joomla!

Joomla! 1.5 Documentation

Packages

Package: Joomla-Framework

License

Content on this site is copyright © 2005 - 2008 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution- NonCommercial- ShareAlike 2.5. Some parts of this website may be subject to other licenses.
Source code for file /joomla/base/object.php

Documentation is available at object.php

  1. <?php
  2. /**
  3.  * @version        $Id: object.php 9764 2007-12-30 07:48:11Z ircmaxell $
  4.  * @package        Joomla.Framework
  5.  * @subpackage    Base
  6.  * @copyright    Copyright (C) 2005 - 2008 Open Source Matters. All rights reserved.
  7.  * @license        GNU/GPL, see LICENSE.php
  8.  *  Joomla! is free software. This version may have been modified pursuant
  9.  *  to the GNU General Public License, and as distributed it includes or
  10.  *  is derivative of works licensed under the GNU General Public License or
  11.  *  other free or open source software licenses.
  12.  *  See COPYRIGHT.php for copyright notices and details.
  13.  */
  14.  
  15. /**
  16.  * Object class, allowing __construct in PHP4.
  17.  *
  18.  * @author        Johan Janssens <johan.janssens@joomla.org>
  19.  * @package        Joomla.Framework
  20.  * @subpackage    Base
  21.  * @since        1.5
  22.  */
  23. class JObject
  24. {
  25.  
  26.     /**
  27.      * An array of errors
  28.      *
  29.      * @var        array of error messages or JExceptions objects
  30.      * @access    protected
  31.      * @since    1.0
  32.      */
  33.     var        $_errors        = array();
  34.  
  35.     /**
  36.      * A hack to support __construct() on PHP 4
  37.      *
  38.      * Hint: descendant classes have no PHP4 class_name() constructors,
  39.      * so this constructor gets called first and calls the top-layer __construct()
  40.      * which (if present) should call parent::__construct()
  41.      *
  42.      * @access    public
  43.      * @return    Object 
  44.      * @since    1.5
  45.      */
  46.     function JObject()
  47.     {
  48.         $args func_get_args();
  49.         call_user_func_array(array(&$this'__construct')$args);
  50.     }
  51.  
  52.     /**
  53.      * Class constructor, overridden in descendant classes.
  54.      *
  55.      * @access    protected
  56.      * @since    1.5
  57.      */
  58.     function __construct({}
  59.  
  60.  
  61.     /**
  62.      * Returns a property of the object or the default value if the property is not set.
  63.      *
  64.      * @access    public
  65.      * @param    string $property The name of the property
  66.      * @param    mixed  $default The default value
  67.      * @return    mixed The value of the property
  68.      * @see        getProperties()
  69.      * @since    1.5
  70.       */
  71.     function get($property$default=null)
  72.     {
  73.         if(isset($this->$property)) {
  74.             return $this->$property;
  75.         }
  76.         return $default;
  77.     }
  78.  
  79.     /**
  80.      * Returns an associative array of object properties
  81.      *
  82.      * @access    public
  83.      * @param    boolean $public If true, returns only the public properties
  84.      * @return    array 
  85.      * @see        get()
  86.      * @since    1.5
  87.       */
  88.     function getProperties$public true )
  89.     {
  90.         $vars  get_object_vars($this);
  91.  
  92.         if($public)
  93.         {
  94.             foreach ($vars as $key => $value)
  95.             {
  96.                 if ('_' == substr($key01)) {
  97.                     unset($vars[$key]);
  98.                 }
  99.             }
  100.         }
  101.  
  102.         return $vars;
  103.     }
  104.  
  105.     /**
  106.      * Get the most recent error message
  107.      *
  108.      * @param    integer    $i Option error index
  109.      * @param    boolean    $toString Indicates if JError objects should return their error message
  110.      * @return    string    Error message
  111.      * @access    public
  112.      * @since    1.5
  113.      */
  114.     function getError($i null$toString true )
  115.     {
  116.         // Find the error
  117.         if $i === null{
  118.             // Default, return the last message
  119.             $error end($this->_errors);
  120.         }
  121.         else
  122.         if array_key_exists($i$this->_errors) ) {
  123.             // If $i has been specified but does not exist, return false
  124.             return false;
  125.         }
  126.         else {
  127.             $error    $this->_errors[$i];
  128.         }
  129.  
  130.         // Check if only the string is requested
  131.         if JError::isError($error&& $toString {
  132.             return $error->toString();
  133.         }
  134.  
  135.         return $error;
  136.     }
  137.  
  138.     /**
  139.      * Return all errors, if any
  140.      *
  141.      * @access    public
  142.      * @return    array    Array of error messages or JErrors
  143.      * @since    1.5
  144.      */
  145.     function getErrors()
  146.     {
  147.         return $this->_errors;
  148.     }
  149.  
  150.  
  151.     /**
  152.      * Modifies a property of the object, creating it if it does not already exist.
  153.      *
  154.      * @access    public
  155.      * @param    string $property The name of the property
  156.      * @param    mixed  $value The value of the property to set
  157.      * @return    mixed Previous value of the property
  158.      * @see        setProperties()
  159.      * @since    1.5
  160.      */
  161.     function set$property$value null )
  162.     {
  163.         $previous = isset($this->$property$this->$property null;
  164.         $this->$property $value;
  165.         return $previous;
  166.     }
  167.  
  168.     /**
  169.     * Set the object properties based on a named array/hash
  170.     *
  171.     * @access    protected
  172.     * @param    $array  mixed Either and associative array or another object
  173.     * @return    boolean 
  174.     * @see        set()
  175.     * @since    1.5
  176.     */
  177.     function setProperties$properties )
  178.     {
  179.         $properties = (array) $properties//cast to an array
  180.  
  181.         if (is_array($properties))
  182.         {
  183.             foreach ($properties as $k => $v{
  184.                 $this->$k $v;
  185.             }
  186.  
  187.             return true;
  188.         }
  189.  
  190.         return false;
  191.     }
  192.  
  193.     /**
  194.      * Add an error message
  195.      *
  196.      * @param    string $error Error message
  197.      * @access    public
  198.      * @since    1.0
  199.      */
  200.     function setError($error)
  201.     {
  202.         array_push($this->_errors$error);
  203.     }
  204.  
  205.     /**
  206.      * Object-to-string conversion.
  207.      * Each class can override it as necessary.
  208.      *
  209.      * @access    public
  210.      * @return    string This name of this class
  211.      * @since    1.5
  212.       */
  213.     function toString()
  214.     {
  215.         return get_class($this);
  216.     }
  217.  
  218.     /**
  219.      * Legacy Method, use {@link JObject::getProperties()}  instead
  220.      *
  221.      * @deprecated as of 1.5
  222.      * @since 1.0
  223.      */
  224.     function getPublicProperties()
  225.     {
  226.         return $this->getProperties();
  227.     }
  228. }

Documentation generated on Tue, 29 Jan 2008 18:48:42 +0000 by phpDocumentor 1.3.1