I can't create object php - php

I am just starting with php and I can't create this object.
I call my object with the variable $theClass is a concatenation of a namespace and a variable from a an array.
One var_dump of $theclass show me the right path... the problem start wen I try to create a new $theClass on variable($control) the var_dump show me nothing...
<?php
namespace App\Router;
class Router {
private $routes = [];
private $url;
public function __construct($url){
$this->url = $url;
}
public function get($path, $action){
$this->routes['GET'][$path] = $action;
}
public function match(){
foreach ($this->routes as $key => $routes) {
foreach ($routes as $path => $action) {
if ($this->url === $path) {
$elements = explode('#', $action);
//$this->callController($elements);
$theClass = "App\Controller\\$elements[0]";
// I try this way to 'App\Controller\\' . $elements[0];
var_dump($theClass);
$method = $elements[1];
var_dump($method);
$control = new $theClass();
$control->$method();
}
}
header('HTTP/1.0 404 Not Found');
}
}
}

Related

Cannot use [] for reading when including

I have this PHP file:
<?php
namespace FrameWork\Controller;
abstract class ControllerBase
{
protected $action;
protected $vars;
public function __construct($action, $vars = NULL)
{
$this->action = $action;
$this->vars = $vars;
$this->populateVars();
}
public function run()
{
****BETWEEN HERE****
$r = new \ReflectionMethod($this, $this->action);
$params = $r->getParameters();
$funcParams[];
foreach($params as $param)
{
$paramName = $param->getName();
$funcParams[$paramName] = $this->vars[$paramName];
}
****AND HERE****
call_user_func_array(array($this, $this->action), $funcParams);
}
private function PopulateVars()
{
foreach($_GET as $key => $getVar)
{
$this->vars[$key] = $getVar;
}
foreach($_POST as $key => $postVar)
{
$this->vars[$key] = $postVar;
}
}
}
It is includeed in another file, and for some reason I am getting an exception thrown on the include.
Cannot use [] for reading
When I remove everything between ****BETWEEN HERE**** and ****AND HERE****, it works (or at least doesn't throw the same exception).
Any idea what's going on?
Have you tried replacing
$funcParams[];
with
$funcParams = array();
I think it will solve your problem.

PHP get dynamically the namespace off a class by concating it

I have a string, route, and i explode it and then i have a classname. $callback = explode('#', $callback); $callback[0] is the classname. I am making a router and based on the class and methed i return this.
I want to get the full namespace and when i use this then it works but now i want to make it dynamically.
How do i concat BaseController to the ::class?
BasController::class
$fullclass = HERE I NEED THE FULL NAMESPACE$callback[0];
$class = new $fullclass;
$method = $callback[1];
$class->$method();
Exaple index.php
Router::route('/user/{id}/' , 'BaseController#show');
Router::execute($_SERVER['REQUEST_URI']);
Router:
namespace App;
class Router
{
private static $routes = array();
private function __construct()
{
}
private function __clone()
{
}
public static function route($pattern, $callback)
{
$pattern = $pattern;
self::$routes[$pattern] = $callback;
}
public static function execute($url)
{
foreach (self::$routes as $pattern => $callback) {
if ($pattern == $url) {
$callback = explode('#', $callback);
$fullclass = __NAMESPACE__ . '\\Controllers\\' . $callback[0];
$class = new $fullclass;
$method = $callback[1];
$class->$method();
}else{
echo 404;
}
}
}
}
BasController::class returns a full class name in string in that case you can simply concat any string on it. If $callback[0] is a string you can do below code.
$fullclass = BasController::class . $callback[0];
$class = new $fullclass;
$method = $callback[1];
$class->$method();
I hope you are trying to retrieve namespace
echo (new \ReflectionClass($object))->getNamespaceName() ;
If you are in that scope try
__NAMESAPCE__
See this document if you are looking for autoload
What i was looking for is giving the class as param into the route
Router::route('/user/{id}/' , App\Controllers\BaseController::class , 'show');

Dynamic instantation of namespaced methods in php

There are few routers out there but I decided to create a very simple route for a very light site.
Here is my index.php
$route = new Route();
$route->add('/', 'Home');
$route->add('/about', 'About');
$route->add('/contact', 'Contact');
Here is my router:
<?php namespace Laws\Route;
use Laws\Controller\Home;
class Route
{
private $_uri = array();
private $_method = array();
private $_route;
public function __construct()
{
}
public function add($uri, $method = null)
{
$this->_uri[] = '/' . trim($uri, '/');
if ($method != null) {
$this->_method[] = $method;
}
}
public function submit()
{
$uriGetParam = isset($_GET['uri']) ? '/' . $_GET['uri'] : '/';
foreach ($this->_uri as $key => $value) {
if (preg_match("#^$value$#", $uriGetParam)) {
$useMethod = $this->_method[$key];
new $useMethod(); // this returns an error (cannot find Home'
new Home(); // this actually works.
}
}
}
}
new $useMethod(); does not work. returns error 'cannot find Home'
new Home(); actually works.
What am I missing here?
You can use your concurrent way for calling a class or you can use this:
call_user_func(array($classname,$methodname))

zend framework 2 + custom routing

I tried to follow the recommendations from this topic: zend framework 2 + routing database
I have a route class:
namespace Application\Router;
use Zend\Mvc\Router\Http\RouteInterface;
use Zend\ServiceManager\ServiceLocatorAwareInterface;
use Zend\Mvc\Router\RouteMatch;
class Content implements RouteInterface, ServiceLocatorAwareInterface {
protected $defaults = array();
protected $routerPluginManager = null;
public function __construct(array $defaults = array()) {
$this->defaults = $defaults;
}
public function setServiceLocator(\Zend\ServiceManager\ServiceLocatorInterface $routerPluginManager) {
$this->routerPluginManager = $routerPluginManager;
}
public function getServiceLocator() {
return $this->routerPluginManager;
}
public static function factory($options = array()) {
if ($options instanceof \Traversable) {
$options = ArrayUtils::iteratorToArray($options);
} elseif (!is_array($options)) {
throw new InvalidArgumentException(__METHOD__ . ' expects an array or Traversable set of options');
}
if (!isset($options['defaults'])) {
$options['defaults'] = array();
}
return new static($options['defaults']);
}
public function match(Request $request, $pathOffset = null) {
if (!method_exists($request, 'getUri')) {
return null;
}
$uri = $request->getUri();
$fullPath = $uri->getPath();
$path = substr($fullPath, $pathOffset);
$alias = trim($path, '/');
$options = $this->defaults;
$options = array_merge($options, array(
'path' => $alias
));
return new RouteMatch($options);
}
public function assemble(array $params = array(), array $options = array()) {
if (array_key_exists('path', $params)) {
return '/' . $params['path'];
}
return '/';
}
public function getAssembledParams() {
return array();
}
}
Pay attention that the match() function returns object of the instance of Zend\Mvc\Router\RouteMatch
However in the file Zend\Mvc\Router\Http\TreeRouteStack it checks for object to be the instance of RouteMatch (without prefix of namespace)
if (
($match = $route->match($request, $baseUrlLength, $options)) instanceof RouteMatch
&& ($pathLength === null || $match->getLength() === $pathLength)
)
And the condition fails in my case because of the namespace.
Any suggestions?
Ok, i figured out what the problem was.
Instead of returning Zend\Mvc\Router\RouteMatch I should return Zend\Mvc\Router\Http\RouteMatch
This fixed my problem

Use Reflection class to pass exact number of parameters to class constructor PHP

I am creating my own MVC framework and the way I handle creation of models is as follows:
class ModelFactory {
public function __construct() {
parent::__construct();
}
public static function Create($model, $params = array()) {
if ( ! empty($model)) {
$model = ucfirst(strtolower($model));
$path = 'application/models/' . $model . '.php';
if (file_exists($path)) {
$path = rtrim(str_replace("/", "\\", $path), '.php');
$modelConstruct = new \ReflectionMethod($path, '__construct');
$numParams = $modelConstruct->getNumberOfParameters();
//fill up missing holes with null values
if (count($params) != $numParams) {
$tempArray = array_fill(0, $numParams, null);
$params = ($params + $tempArray);
}
//instead of thi
return new $path($params);
//I want to DO THIS
return new $path($param1, $param2, $param3 ... $paramN)
//where $paramN is the last value from $params array
}
}
return null;
}
}
a simple Model example:
class UsersModel {
public function __construct($userID, $userName) {
//values of these parameters should be passed from Create function
var_dump($userID, $userName);
}
}
Solved:
Thanks to schokocappucino & pozs I fixed it by doing this:
$modelConstruct = new \ReflectionMethod($path, '__construct');
$numParams = $modelConstruct->getNumberOfParameters();
if (count($params) != $numParams) {
$tempArray = array_fill(0, $numParams, '');
$params = ($params + $tempArray);
}
return (new \ReflectionClass($path))->newInstanceArgs($params);
To get the constructor of a class using reflection, use ReflectionClass::getConstructor().
To create a new instance (with the constructor) using an argument list, use ReflectionClass::newInstanceArgs()
return (new ReflectionClass($path))->newInstanceArgs($params);

Categories