Support Joomla!

Packages

Package: Joomla-Framework

License

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

Documentation generated on Sat, 14 Nov 2009 11:16:24 +0000 by phpDocumentor 1.3.1