Jump to content

Recommended Posts

Hello i have the following error in php using this class.

It seems it's because of the new modifications in php >= 5.2, how it handles this.

I have no idea how to solve this problem tried with ArrayObject but didn't manage doing anything.

I hope someone from here might help me 

Thank  you very much for taking time and reading this post / answering.

Notice: Indirect modification of overloaded element of Library\Config\Config has no effect in subdomains/prod/index.php on line 33
 
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]);
}
}
Link to comment
https://forums.phpfreaks.com/topic/278584-magic-methods-overload-fail/
Share on other sites

Ok i've posted the latest file version after trying to implement ArrayObject... because of my google search .. found an article saying this would be the solution.

I've posted the way i used it.

I've posted the output.

Anyway can help me out ?

 

Usage:

$config->dumpArray();
echo $config['dbSettings']['dsn'];
$config['dbSettings']['dsn'] = 'localhost';
echo $config->dbSettings->dsn;

Output:

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

Class Code:

<?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 "<pre>";
        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]);
    }
}

$config['dbSettings']['dsn'] = 'localhost';
$config['dbSettings'] is actually a return value from a function. You can't modify it, but you can copy the value to a variable, modify it there, and put the value back (via another function call).

$array = $config['dbSettings'];
$array['dsn'] = 'localhost';
$config['dbSettings'] = $array;
$config['dbSettings']['dsn'] = 'localhost';
$config['dbSettings'] is actually a return value from a function. You can't modify it, but you can copy the value to a variable, modify it there, and put the value back (via another function call).

$array = $config['dbSettings'];
$array['dsn'] = 'localhost';
$config['dbSettings'] = $array;

This is the only way since the new modifications to __set & __get from php 5.2 ? 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.