4

MVC モジュールなしで zf2 から ServiceManager を使用しようとしています。classServiceManager.php と sm.php の 2 つのファイルがあります。

1) classServiceManager.php:

namespace ZF2;

use Zend\ServiceManager\ServiceManager; 
use Zend\ServiceManager\ServiceLocatorAwareInterface; 
use Zend\ServiceManager\ServiceLocatorInterface; 
use \InvalidArgumentException;  

class classServiceManager implements ServiceLocatorAwareInterface{
    protected $serviceLocator;

    function __construct(){
        //echo "SM: ". $this->getServiceLocator()->get('sm');
        echo "SM: ".$this->serviceLocator->get('sm');

    }

    function setServiceLocator(ServiceLocatorInterface $serviceLocator) {
        $this->serviceLocator = $serviceLocator;
      }

    function getServiceLocator() {
        return $this->serviceLocator;
    }       

}

2) sm.php

$config = array(...);

require_once 'Zend/Loader/AutoloaderFactory.php';
Zend\Loader\AutoloaderFactory::factory($config); 
......  
use ZF2\classServiceManager;

$serviceManager = new ServiceManager();
$serviceManager->setService('sm', 'aaa');   

$a = new classServiceManager();

しかし、実行sm.phpするとエラーが発生します。

Fatal error: Call to a member function get() on a non-object....
4

2 に答える 2