在子对象

时间:2016-01-12 21:50:45

标签: php

我想将$ this传递给我在construct创建的子对象。但是当我转储它时,我得到一个**RECURSION**标志。

<?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;
    }
}

1 个答案:

答案 0 :(得分:0)

当您创建一个扩展其他类的新类时,您不需要将任何内容传递给构造函数,只要这些方法/属性是公共的或受保护的,它就会自动在子类中可用。如果它们是私有的,则不能在子类中使用它们。如果要在子类中使用父类的受保护/公共属性或方法,请使用它们,就像使用父类一样。

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'