php - Script Scope Access -
so, i've been working on small project, nothing extensive. i've been having trouble understand why function in anonymous object isn't accessible on main script.
alright "initializer", or script loaded before crucial operations, instantiates anonymous object accessed across entire scope of project.
<?php class t_empty { public function __call($method, $args) { if(isset($this->$method) && is_callable($this->$method)) { return call_user_func_array( $this->$method, $args ); } } } $scope = new t_empty; $scope->config = array( 'path' => $_server['document_root'], 'loct' => '*' ); set_include_path ( $scope->config['path'] ); $scope->controller = $scope->service = $scope->service->injector = new t_empty; // # instantiate our empty structures $scope->service->injector->inject = function($rsc) { global $scope; if( is_array( $rsc ) && count( $rsc ) >= 1 ): foreach( $rsc $r ): require_once( $scope->config['path'] . '/api/services/' . $r . '.php' ); endforeach; else: require_once( $scope->config['path'] . '/api/services/' . $rsc . '.php' ); endif; }; $scope->service->injector->f = "foobar";
the issue standing, after importing main script via
require_once
i cannot access 'inject' function.
global $scope; $f = & $scope->service->injector; $f->inject( 'communicator' ); // # function marked undefined echo $f->f; // element exists
the function whatever reason undefined, can access 'f' without trouble.
the problem $scope->service
doesn't have injector
property. chained assignment initializes $scope->service->injector
isn't doing think, because assignments associate right left. so
$scope->controller = $scope->service = $scope->service->injector = new t_empty;
is executed if it's
$temp1 = $scope->service->injector = new t_empty; $temp2 = $scope->service = $temp1; $scope->controller = $temp2;
the assignment $scope->service
overwrites $scope->service
object created implicitly assignment $scope->service->injector
new t_empty
instance, , doesn't have injector
property.
the other problem chained assignment you're setting them same t_empty
instance (objects aren't copied when they're assigned, arrays are), not wanted. change line to:
$scope->controller = new t_empty; $scope->service = new t_empty; $scope->service->injector = new t_empty;
Comments
Post a Comment