vendredi 19 septembre 2014

PHP - Can't assign values to a class variable in __construct()


Vote count:

0




First of all, sorry if my english is bad.


I'm making a REST API Server in PHP using OOP Paradigm. But, I'm having troubles when i try to assign the $_params array in the __construct method of Addestablishment class (I made a var_dump($this->$_params) and the system printed me NULL)


This is the URL that i'm passing



http://ift.tt/1uPjMx3


So, i append the code



include_once 'models/Addestablishment_Model.php';

try {
$params = $_REQUEST;

$controller = ucfirst(strtolower($params['controller']));

$action = strtolower($params['action']).'Action';

if( file_exists("controllers/{$controller}.php") ) {
include_once "controllers/{$controller}.php";
} else {
throw new Exception('Controller is invalid.');
}

$controller = new $controller($params);

if( method_exists($controller, $action) === false ) {
throw new Exception('Action is invalid.');
}

//execute the action
$result['data'] = $controller->$action();
$result['success'] = true;

} catch( Exception $e ) {
$result = array();
$result['success'] = false;
$result['errormsg'] = $e->getMessage();
}

echo json_encode($result);
exit();


And, in the $action method, i have the following code (my controller class)



class Addestablishment
{
private $_params = array();

public function __construct($params)
{
$this->_params = $params;
var_dump($this->_params); //i got null here :(
}

public function createAction()
{
$obj_NewEstablishment = new Addestablishment_Model(
$this->_params['name'],
$this->_params['type'],
$this->_params['address'],
$this->_params['location'],
$this->_params['zip'],
$this->_params['state'],
$this->_params['country']
);


return $obj_NewEstablishment->toArray();
}


And the response of my api is the next



{"data":{"establishment_name":null,"establishment_type":null,"establishment_address":null,"establishment_location":null,"establishment_zip":null,"establishment_state":null,"establishment_country":null},"success":true}


Thanks for reading my question, maybe is an stu**d mistake but I'm newbie in PHP - OOP and programming RESTful API



asked 42 secs ago







PHP - Can't assign values to a class variable in __construct()

Aucun commentaire:

Enregistrer un commentaire