php notice error >= 5.2

Hello i have the following problem with a class. Seems that i don’t know how to fix this problem i’m out of ideas any one here who might help me out fixing using some great solution that i haven’t seen it ? Tried using ArrayObject but i couldn’t make the right implementation.

Line Code: 33

[php]$config[‘url’][‘uri’] = $request->uri();[/php]

PHP Error:

[php]Notice: Indirect modification of overloaded element of Library\Config\Config has no effect in subdomains/prod/index.php on line 33
[/php]

Config Class:

[php]use \ArrayObject;
use \ArrayAccess;
use \Countable;
use \IteratorAggregate;
use \ArrayIterator;
use \Twelve\SingletonPattern\LazySingleton;
use \Twelve\SingletonPattern\Interfaces\ILazySingleton;

/**

  • Singleton With Configuration Info
    /
    class Config extends ArrayObject implements ArrayAccess, Countable, IteratorAggregate, ILazySingleton
    {
    /
    *
  • Instance Var
  • @var Config
    */
    protected static $_instance = null;

/**

  • Stores FileName
  • @var Config
    */
    protected static $_configFile = ‘’;

/**

  • Config Settings Array
  • @var Config
    */
    protected $_settings = array();

public static function getInstance(){
return LazySingleton::getInstance(CLASS);
}

/**

  • Set the Config File Path
    */
    public static function setFile($filePath)
    {
    static::$_configFile = $filePath;
    }

public function __construct()
{
LazySingleton::validate(CLASS);
// Allow accessing properties as either array keys or object properties:
parent::__construct($this->_settings, ArrayObject::ARRAY_AS_PROPS);
$values = include(static::$_configFile);
if(is_array($values))
{
$this->_settings = $values;
}
}

/**

  • Prevent Cloning
    */
    public function __clone()
    {
    trigger_error(‘Clone is not allowed.’, E_USER_ERROR);

// No Cloning Allowed
}

/**

  • Returns the Number of Elements Inside the Config File
  • @var Config
  • @return Integer Number of Elements Inside Config
    */
    public function count()
    {
    return sizeof($this->_settings);
    }

/**

  • Check if a Given Key Exists
  • @var Config
  • @param mixed $offset Key of Item to Check
  • @return Boolean True if Key Exists, False Otherwise
    */
    public function offsetExists($offset)
    {
    return key_exists($offset, $this->_settings);
    }

/**

  • Retrieve the Value of a Given Key
  • @param mixed $offset Key of Item to Fetch
  • @return mixed Value of the Matched Element
    */
    public function offsetGet($offset)
    {
    return $this->_settings[$offset];
    }

/**

  • Assign a new Value to a Key
  • @param mixed $offset Key of the Element to Set
  • @param mixed $value Value to Assign
    */
    public function offsetSet($offset, $value)
    {
    $this->_settings[$offset] = $value;
    }

/**

  • Remove an Item from the Config
  • @param mixed $offset Key of the Element to Remove
    */
    public function offsetUnset($offset)
    {
    unset($this->_settings[$offset]);
    }

/**

  • Retrieve an Iterator for Config Values
  • @return Iterator Iterator of Config Values
    */
    public function getIterator()
    {
    return new ArrayIterator($this->_settings);
    }

/**

  • Enables to Set Values Using the Object Notation i.e $config->myValue = ‘Something’;
    */
    public function __set($key, $value)
    {
    (array) $this->_settings[$key] = $value;
    }

/**

  • Enables to Get Values using the Object Notation i.e $config->myValue;
    */

public function &__get($key)
{
return $this->_settings[$key];
}

public function __isset($key)
{
return isset($this->_settings[$key]);
}
}[/php]

This is due to your implementation of the ArrayAccess interface. Depending on the actual code that parent::__construct() performs, I have a feeling that $this->_settings is no longer an array. Could you confirm?

I’ve added a small array as an example with some stuff it came to my mind… seems it’s an array type. with the code above.

[php]Array
(
[session] => Array
(
[sessionLifetime] => 60
[gcProbability] => 100
[gcDivisor] => 200
[securityCode] => SD&*$&@#sadux&D@3232sd
)

[dbSettings] => Array
    (
        [dsn] => mysql:dbname=Twelve;host=localhost
        [username] => root
        [password] => password
        [lockTimeOut] => 50
    )

)[/php]

This leads to the logical question: can you check that $this->_settings[‘url’] actually is defined? The ArrayAccess implementation won’t do this for you. If this is not true, you’ll effectively try to access the key [‘uri’] of null, which will fail.

ok so here is my latest code version after tried few things.

Usage:

[php]$config->dumpArray();
echo $config[‘dbSettings’][‘dsn’];
$config[‘dbSettings’][‘dsn’] = ‘localhost’;
echo $config->dbSettings->dsn;[/php]

Output:
[php]
ArrayObject Object
(
[storage:ArrayObject:private] => Array
(
[session] => Array
(
[sessionLifetime] => 60
[gcProbability] => 100
[gcDivisor] => 200
[securityCode] => SD&*$&@#sadux&D@3@#@#@SD
)

        [dbSettings] => Array
            (
                [dsn] => mysql:dbname=Test;host=localhost
                [username] => root
                [password] => somepasswordhere
                [lockTimeOut] => 50
            )

    )

)
mysql:dbname=Twelve;host=localhost

Notice: Indirect modification of overloaded element of Twelve\Config\Config has no effect in /home/subdomains/prod/index.php on line 34

Notice: Trying to get property of non-object in /home/subdomains/prod/index.php on line 35[/php]

Class Code:

[php]<?php
namespace Twelve\Config;
use \ArrayObject;
use \ArrayAccess;
use \Countable;
use \IteratorAggregate;
use \ArrayIterator;
use \Twelve\SingletonPattern\LazySingleton;
use \Twelve\SingletonPattern\Interfaces\ILazySingleton;

/**

  • Singleton With Configuration Info
    /
    class Config extends ArrayObject implements ArrayAccess, Countable, IteratorAggregate, ILazySingleton
    {
    /
    *

    • Instance Var
    • @var Config
      */
      protected static $_instance = null;

    /**

    • Stores FileName
    • @var Config
      */
      protected static $_configFile = ‘’;

    /**

    • Config Settings Array
    • @var Config
      */
      protected $_settings = array();

    public static function getInstance(){
    return LazySingleton::getInstance(CLASS);
    }

    /**

    • Set the Config File Path
      */
      public static function setFile($filePath)
      {
      static::$_configFile = $filePath;
      }

    public function __construct()
    {
    LazySingleton::validate(CLASS);

    // Allow accessing properties as either array keys or object properties:
    //parent::__construct($this->_settings, ArrayObject::ARRAY_AS_PROPS);
    $values = include(static::$_configFile);
    if(is_array($values))
    {
    	$this->_settings = new ArrayObject(&$values, ArrayObject::ARRAY_AS_PROPS);
    }
    

    }

    // some debug method
    public function dumpArray()
    {
    echo “

    ”;
    print_r($this->_settings);
    }

    /**

    • Prevent Cloning
      */
      public function __clone()
      {
      trigger_error(‘Clone is not allowed.’, E_USER_ERROR);

      // No Cloning Allowed
      }

    /**

    • Returns the Number of Elements Inside the Config File
    • @var Config
    • @return Integer Number of Elements Inside Config
      */
      public function count()
      {
      return sizeof($this->_settings);
      }

    /**

    • Check if a Given Key Exists
    • @var Config
    • @param mixed $offset Key of Item to Check
    • @return Boolean True if Key Exists, False Otherwise
      */
      public function offsetExists($offset)
      {
      return key_exists($offset, $this->_settings);
      }

    /**

    • Retrieve the Value of a Given Key
    • @param mixed $offset Key of Item to Fetch
    • @return mixed Value of the Matched Element
      */
      public function offsetGet($offset)
      {
      return $this->_settings[$offset];
      }

    /**

    • Assign a new Value to a Key
    • @param mixed $offset Key of the Element to Set
    • @param mixed $value Value to Assign
      */
      public function offsetSet($offset, $value)
      {
      $this->_settings[$offset] = $value;
      }

    /**

    • Remove an Item from the Config
    • @param mixed $offset Key of the Element to Remove
      */
      public function offsetUnset($offset)
      {
      unset($this->_settings[$offset]);
      }

    /**

    • Retrieve an Iterator for Config Values
    • @return Iterator Iterator of Config Values
      */
      public function getIterator()
      {
      return new ArrayIterator($this->_settings);
      }

    /**

    • Enables to Set Values Using the Object Notation i.e $config->myValue = ‘Something’;
      */
      public function __set($key, $value)
      {
      $this->_settings[$key] = $value;
      }

    /**

    • Enables to Get Values using the Object Notation i.e $config->myValue;
      */

    public function __get($key)
    {
    return $this->_settings[$key];
    }

    public function __isset($key)
    {
    return isset($this->_settings[$key]);
    }
    }[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service