2

I want to pass $this into the sub objects I create on construct However when I dump it I get a **RECURSION** flag.

<?php
namespace Cortana\Framework\System;

use Cortana\Framework\System\ApiAccessor\CacheApiAccessor;
use Cortana\Framework\System\ApiAccessor\ConfigurationApiAccessor;
use Cortana\Framework\System\ApiAccessor\DatabaseApiAccessor;
use Cortana\Framework\System\ApiAccessor\EventsManagerApiAccessor;
use Cortana\Framework\System\ApiAccessor\FilesystemApiAccessor;
use Cortana\Framework\System\ApiAccessor\LocaleApiAccessor;
use Cortana\Framework\System\ApiAccessor\LogApiAccessor;
use Cortana\Framework\System\ApiAccessor\SessionApiAccessor;
use Cortana\Framework\System\ApiAccessor\TranslateApiAccessor;

/**
 * System Handler Instance
 * 
 * @author root
 */
class SystemHandler
{
    /**
     * Development Environment Flag
     * 
     * @var string
     */
    const ENV_DEVELOPMENT = 'DEVELOPMENT';

    /**
     * Staging Environment Flag
     *
     * @var string
     */
    const ENV_STAGING = 'STAGING';

    /**
     * Test Environment Flag
     *
     * @var string
     */
    const ENV_TEST = 'TEST';

    /**
     * Production Environment Flag
     *
     * @var string
     */
    const ENV_PRODUCTION = 'PRODUCTION';

    /**
     * Configuration api accessor instance
     * 
     * @var ConfigurationApiAccessor
     */
    protected $configurationApiAccessor;

    /**
     * Database api accessor instance
     *
     * @var DatabaseApiAccessor
     */
    protected $databaseApiAccessor;

    /**
     * Translation api accessor instance
     *
     * @var TranslateApiAccessor
     */
    protected $translateApiAccessor;

    /**
     * Cache api accessor instance
     *
     * @var CacheApiAccessor
     */
    protected $cacheApiAccessor;

    /**
     * Locale api accessor instance
     *
     * @var LocaleApiAccessor
     */
    protected $localeApiAccessor;

    /**
     * Events Manager api accessor instance
     *
     * @var EventsManagerApiAccessor
     */
    protected $eventsManagerApiAccessor;

    /**
     * Log api accessor instance
     *
     * @var LogApiAccessor
     */
    protected $logApiAccessor;

    /**
     * Session api accessor instance
     *
     * @var SessionApiAccessor
     */
    protected $sessionApiAccessor;

    /**
     * Filesystem api accessor instance
     *
     * @var FilesystemApiAccessor
     */
    protected $filesystemApiAccessor;

    /**
     * Array of Loaded Modules 
     *
     * @var array
     */
    protected $loadedModules=[];

    /**
     * System Environment
     * 
     * @var string
     */
    protected $environment;

    /**
     * System Handler Constructor. Loads Accessors
     */
    public function __construct()
    {
        $this->filesystemApiAccessor = new FilesystemApiAccessor();
        $this->configurationApiAccessor = new ConfigurationApiAccessor();
        $this->databaseApiAccessor = new DatabaseApiAccessor();
        $this->cacheApiAccessor = new CacheApiAccessor();
        $this->eventsManagerApiAccessor = new EventsManagerApiAccessor();
        $this->localeApiAccessor = new LocaleApiAccessor();
        $this->logApiAccessor = new LogApiAccessor();
        $this->sessionApiAccessor = new SessionApiAccessor();
        $this->translateApiAccessor = new TranslateApiAccessor();
    }

    public function build()
    {

    }

    /**
     * Set the system environment
     * 
     * @param string $environment the system environment
     * @return SystemHandler
     */
    public function setEnvironment($environment)
    {
        $this->environment = $environment;
        return $this;
    }

    /**
     * Return the system environment
     * 
     * @return string the system environment
     */
    public function getEnvironment()
    {
        return $this->environment;  
    }

    /**
     * Set the system loaded modules
     *
     * @param array $loadedmodules array of loaded system modules
     * @return SystemHandler
     */
    public function setLoadedModules(array $loadedmodules)
    {
        $this->loadedModules = $loadedmodules;
        return $this;
    }

    /**
     * Add a module as a loaded module
     * 
     * @param string $module module to add as a loaded module
     * @return SystemHandler
     */
    public function addLoadedModule($module)
    {
        $this->loadedModules[] = $module;
        return $this;
    }

    /**
     * Clear loaded modules
     * 
     * @return SystemHandler
     */
    public function clearLoadedModule()
    {
        $this->loadedModules = [];
        return $this;
    }

    /**
     * Set the configuration api accessor instance
     * 
     * @param ConfigurationApiAccessor $configurationApiAccessor the configuration api accessor instance
     * @return SystemHandler
     */
    public function setConfigurationApiAccessor(ConfigurationApiAccessor $configurationApiAccessor)
    {
        $this->configurationApiAccessor = $configurationApiAccessor;
        return $this;
    }

    /**
     * Return the configuration api accessor instance
     * 
     * @return ConfigurationApiAccessor the configuration api accessor instance
     */
    public function getConfigurationApiAccessor()
    {
        return $this->configurationApiAccessor;
    }

    /**
     * Set the database api accessor instance
     *
     * @param DatabaseApiAccessor $databaseApiAccessor the database api accessor instance
     * @return SystemHandler
     */
    public function setDatabaseApiAccessor(DatabaseApiAccessor $databaseApiAccessor)
    {
        $this->databaseApiAccessor = $databaseApiAccessor;
        return $this;
    }

    /**
     * Return the database api accessor instance
     *
     * @return DatabaseApiAccessor the database api accessor instance
     */
    public function getDatabaseApiAccessor()
    {
        return $this->databaseApiAccessor;
    }

    /**
     * Set the translation api accessor instance
     *
     * @param TranslateApiAccessor $translateApiAccessor the translation api accessor instance
     * @return SystemHandler
     */
    public function setTranslateApiAccessor(TranslateApiAccessor $translateApiAccessor)
    {
        $this->translateApiAccessor = $translateApiAccessor;
        return $this;
    }

    /**
     * Return the translation api accessor instance
     *
     * @return TranslateApiAccessor the translation api accessor instance
     */
    public function getTranslateApi()
    {
        return $this->translateApiAccessor;
    }

    /**
     * Set the cache api accessor instance
     *
     * @param CacheApiAccessor $cacheApiAccessor the cache api accessor instance
     * @return SystemHandler
     */
    public function setCacheApiAccessor(CacheApiAccessor $cacheApiAccessor)
    {
        $this->cacheApiAccessor = $cacheApiAccessor;
        return $this;
    }

    /**
     * Return the cache api accessor instance
     *
     * @return CacheApiAccessor the cache api accessor instance
     */
    public function getCacheApiAccessor()
    {
        return $this->cacheApiAccessor;
    }

    /**
     * Set the locale api accessor instance
     *
     * @param LocaleApiAccessor $localeApiAccessor the locale api accessor instance
     * @return SystemHandler
     */
    public function setLocaleApiAccessor(LocaleApi $localeApiAccessor)
    {
        $this->localeApiAccessor = $localeApiAccessor;
        return $this;
    }

    /**
     * Return the locale api accessor instance
     *
     * @return LocaleApiAccessor the locale api accessor instance
     */
    public function getLocaleApiAccessor()
    {
        return $this->localeApi;
    }

    /**
     * Set the events manager api accessor instance
     *
     * @param EventsManagerApiAccessor $eventsManagerApiAccessor the events manager api accessor instance
     * @return SystemHandler
     */
    public function setEventsManagerApiAccessor(EventsManagerApiAccessor $eventsManagerApiAccessor)
    {
        $this->eventsManagerApiAccessor = $eventsManagerApiAccessor;
        return $this;
    }

    /**
     * Return the events manager api accessor instance
     *
     * @return EventsManagerApiAccessor the events manager api accessor instance
     */
    public function getEventsManagerApiAccessor()
    {
        return $this->eventsManagerApiAccessor;
    }

    /**
     * Set the logger api accessor instance
     *
     * @param LogApiAccessor $logApiAccessor the log api accessor instance
     * @return SystemHandler
     */
    public function setLogApi(LogApiAccessor $logApiAccessor)
    {
        $this->logApiAccessor = $logApiAccessor;
        return $this;
    }

    /**
     * Return the log api accessor instance
     *
     * @return LogApiAccessor the log api accessor instance
     */
    public function getLogApi()
    {
        return $this->logApiAccessor;
    }

    /**
     * Set the session api accessor instance
     *
     * @param SessionApiAccessor $sessionApiAccessor the session api accessor instance
     * @return SystemHandler
     */
    public function setSessionApiAccessor(SessionApiAccessor $sessionApiAccessor)
    {
        $this->sessionApiAccessor = $sessionApiAccessor;
        return $this;
    }

    /**
     * Return the session api accessor instance
     *
     * @return SessionApiAccessor the session api accessor instance
     */
    public function getSessionApiAccessor()
    {
        return $this->sessionApiAccessor;
    }

    /**
     * Set the filesystem api accessor instance
     *
     * @param FilesystemApiAccessor $filesystemApiAccessor the filesystem api accessor instance
     * @return SystemHandler
     */
    public function setFilesystemApiAccessor(FilesystemApiAccessor $filesystemApiAccessor)
    {
        $this->filesystemApiAccessor = $filesystemApiAccessor;
        return $this;
    }

    /**
     * Return the filesystem api accessor instance
     *
     * @return FilesystemApiAccessor the filesystem api accessor instance
     */
    public function getFilesystemApiAccessor()
    {
        return $this->filesystemApiAccessor;
    }
}
somejkuser
  • 8,856
  • 20
  • 64
  • 130
  • Are you sure that your setter functions should return `$this` ? – Maximus2012 Jan 12 '16 at 21:52
  • have you tried `parent::function_name();` – Lionel Ritchie the Manatee Jan 12 '16 at 21:52
  • 1
    @Maximus2012 that doesn't matter, it would just be used for method chaining (i.e. `$class->setEnvironment('foo')->setLoadedModule('bar');`). – Sam Jan 12 '16 at 21:53
  • 2
    that's just a dump/display issue. since you're embedding the outer object in the child objects, and var_dump follows the chain top->bottom, it'll notice it's "recursing" when it encounters the stored parent-object-pointer – Marc B Jan 12 '16 at 21:54
  • So I can pass $this into the objects on construct and it will be available? – somejkuser Jan 12 '16 at 21:55
  • yes. http://stackoverflow.com/questions/6292164/using-print-r-and-var-dump-with-circular-reference might be interesting – Gavriel Jan 12 '16 at 21:57
  • Out of curiosity, how come you didn't try it before asking? It's not like your computer would blow up :) – N.B. Jan 12 '16 at 21:58
  • Can you simplify the example code? It seems like all the functions are similar, you didn't need to post them all. – Barmar Jan 12 '16 at 22:49

1 Answers1

0

when you create a new class that extends other class, you don't need to pass anything to the constructor, it will be automatically available in the subclass as long as those methods/properties are public or protected. If they are private, you cannot use them in subclass. If you want to use protected/public properties or methods from parent class in your subclass, use them as if you were in the parent class.

class parent{
 protected $foo = 'foobar';
 protected function get_foo(){
  return $this->foo;
 }
}

class subclass extends parent{

 protected $bar;
 public function __construct(){
   $this->bar = $this->get_foo();
 }
 public function get_bar(){
   return $this->bar;
 }
}


$baz = new bar;
echo $baz->get_bar();

// outputs 'foobar'
JosefPP
  • 642
  • 1
  • 5
  • 11