0

I have been studying the Registry class provided by the book PHP 5 social Networking. It is not clear exactly what it is doing. The usual Registry design pattern is something like the one presented in PHP Registry Pattern. This is one seems different:

class Registry {

    /**
     * Array of objects
     */
    private $objects;

    /**
     * Array of settings
     */
    private $settings;

    public function __construct() {
    }

    /**
     * Create a new object and store it in the registry
     * @param String $object the object file prefix
     * @param String $key pair for the object
     * @return void
     */
    public function createAndStoreObject( $object, $key )
    {
        require_once( $object . '.class.php' );
        $this->objects[ $key ] = new $object( $this );
    }

    /**
     * Get an object from the registries store
     * @param String $key the objects array key
     * @return Object
     */
    public function getObject( $key )
    {
        return $this->objects[ $key ];
    }

}

I am not able to understand the structure "new $object( $this )" which belongs to the function "createAndStoreObject( $object, $key )".

Inside the index.php, this class is used like this:

require('registry/registry.class.php');
$registry = new Registry();

$registry->createAndStoreObject( 'mysqldb', 'db' );

include(FRAMEWORK_PATH . 'config.php'); // Config.php contains the database configuration
// create a database connection
$registry->getObject('db')->newConnection( $configs['db_host_sn'], $configs['db_user_sn'], $configs['db_pass_sn'], $configs['db_name_sn']);

// store settings in our registry
$settingsSQL = "SELECT `key`, `value` FROM settings"; // settings is a table previously defined
$registry->getObject('db')->executeQuery( $settingsSQL );
while( $setting = $registry->getObject('db')->getRows() )
{
    $registry->storeSetting( $setting['value'], $setting['key'] );
}

Or

$registry->createAndStoreObject( 'urlprocessor', 'url' );
$registry->getObject('url')->getURLData();

0) What does it mean that "$this"?

1) Can one explain me this kind of structure in the index file?

2) Is there any readable book that I can use to understand this kind of advanced php?

Community
  • 1
  • 1
DanielTheRocketMan
  • 3,199
  • 5
  • 36
  • 65

1 Answers1

1

$this is a variable that indicates the instance of the class that your program is currently executing in scope i.e. you are inside an instance of that class

before trying to understand a design pattern based on object oriented programming, you need to understand object oriented programming. $this , self::, parent::, static:: and several other language features of php help you use the polymorphic features of an OO language... Read and get a good grasp of OO programming and principles before trying to understand the concepts you are trying to learn here or you will be wasting your time.. It will be like looking at calculus and asking what multiplication symbols are for...

Zak
  • 24,947
  • 11
  • 38
  • 68
  • ,thank u! I understand clearly this "this" in "$this->objects[ $key ]=", I dont understand "=new $object( $this )". They are not pointing to the same variable, arent they? – DanielTheRocketMan Feb 08 '14 at 01:53
  • 1
    yup, same thing. the registry is giving whatever object it creates a reference to itself. now you just have to ask and answer "why?" – Zak Feb 08 '14 at 01:56
  • sorry, @Zak. I didnt use correctly the english structure (They are not pointing to the same variable, arent they?)! Actually, I thought they were not pointing to the same variable. Actually, I never seem this kind of structure before -- it is still very strange. I think I should study more OOP as u suggested. – DanielTheRocketMan Feb 08 '14 at 02:10