I use __remap() function to avoid any undefine method and make it redirect to index() function.
function __remap($method)
{
$array = {"method1","method2"};
in_array($method,$array) ? $this->$method() : $this->index();
}
That function will check if other than method1 and method2.. it will redirect to index function.
Now, how I can automatically grab all public function methods in that controller instead of manually put on $array variable?
You need to test if method exists and is public. So you need use reflection and method exists. Something like this:
function __remap($method)
{
if(method_exists($this, $method)){
$reflection = new ReflectionMethod($this, $method);
if($reflection->isPublic()){
return $this->{$method}();
}
}
return $this->index();
}
Or you can use get_class_methods() for create your array of methods
OK, I was bored:
$r = new ReflectionClass(__CLASS__);
$methods = array_map(function($v) {
return $v->name;
},
$r->getMethods(ReflectionMethod::IS_PUBLIC));
I've modified the codes and become like this.
function _remap($method)
{
$controllers = new ReflectionClass(__CLASS__);
$obj_method_existed = array_map(function($method_existed)
{
return $method_existed;
},
$controllers->getMethods(ReflectionMethod::IS_PUBLIC));
$arr_method = array();
//The following FOREACH I think was not good practice.
foreach($obj_method_existed as $method_existed):
$arr_method[] = $method_existed->name;
endforeach;
in_array($method, $arr_method) ? $this->$method() : $this->index();
}
Any enhancement instead of using foreach?
Related
I am attempting to write a simple wrapper class to get the value of a global variable. I was thinking to use it like this:
print_r($class->session()->getAll());
print_r($class->cookie()->getAll());
Here's what I have:
class GlobalVars() {
private $current;
public function session() {
$this->current = 'SESSION';
return $this;
}
public function cookie() {
$this->current = 'COOKIE';
return $this;
}
public function getAll() {
return $_{$this->current}; // Obviously wrong
}
public function get($key) {
if (!isset($_{$this->current}[$key])) { // Obviously wrong
return false;
}
return $_{$this->current}[$key]; // Obviously wrong
}
public function set($arr) {
if (is_array($arr)) {
foreach ($arr as $k => $v) {
$_{$this->current}[$k] = $v;
}
}
}
}
$class = new GlobalVars();
print_r($class->session()->getAll());
With this example, I get a Notice: Undefined variable: _ message. What do I need to modify to get this to work?
In my opinion this is just a simple syntactical error you have made. What you did:
public function getAll() {
return $_{$this->current}; // Obviously wrong
}
But the correct way to emulate a variable from string is:
public function getAll() {
return ${"_".$this->current};
}
I have tested it. Similar behaviour for the other variables. More information on variable variables in the docs: http://php.net/manual/en/language.variables.variable.php
It's not gonna work like this. You need variable variables:
$var = "_{$this->current}";
var_dump($$var['rnd']);
Example
It's very bad way to use varVars, because it's not readable and usually IDE does not know what are you using and it's easy to get buggy code.
I have a question about getting value from running a method from a string. While I am able to handle a single method call from a string I am curious how to call a chain of methods from a string.
For example. $project is an Object.
$method1 = "name";
$project->$method1; // It shows the valid results
$method2 = "get()->first()->name";
$project->get()->first()-name; // It shows the valid results
$project->$method2; // get a null result
Please help to find a way to make the $method2 work. And what happen if I have params inside those methods?
The reason here is I have made an array of customized methods. It can be run line by line, but I am thinking of a way to turn them into a loop, so it's more efficient. Put the methods in to a file then get values by looping to them.
Array = ["getvalue1()", "getvalue2()",...."getValuen()->anotherMethod()->value"]
Thanks,
If you want nested try something like this:
private function callMethodChain($model, $methodChain)
{
return array_reduce(explode('->', $methodChain), function($model, $method) {
return $model->$method;
}, $model);
}
This will go through a chain of method calls as your described. If some of the chain (the last piece) is a property I think I once rigged up the following to handle it:
protected function callMethodChain($model, $methodChain)
{
return array_reduce(explode('->', $methodChain), function($model, $method) {
try {
return $model->$method;
} catch (Exception $e) {
return $model->$method();
}
}, $model);
}
If you want to add params try replacing $model->method with:
call_user_func_array(
array($project, 'your_method'),
$params
);
Try this approach:
$method1 = 'name';
$project->{$method1}();
Run method from a string value
Use call_user_func() and call_user_func_array().
call_user_func_array() suits good if you are passing parameters
call_user_func_array(
array($project, 'your_method'),
$params
);
Chain function
function chain_fun($chain,$object)
{
return array_reduce(explode('->', $chain), function ($obj, $method) {
if(preg_match('/[()]/',$method)){
$method=trim($method,'()');
return $obj->$method();
}
return $obj->$method;
}, $object);
}
Here is test
akshay#db-3325:/tmp$ cat test.php
<?php
class Testclass
{
private $str;
function __construct()
{
$this->str = new StdClass;
}
function addA()
{
$this->str->a='A';
return $this;
}
function addB()
{
$this->str->b='B';
return $this;
}
function get()
{
return $this->str;
}
}
function chain_fun($chain,$object)
{
return array_reduce(explode('->', $chain), function ($obj, $method) {
if(preg_match('/[()]/',$method)){
$method=trim($method,'()');
return $obj->$method();
}
return $obj->$method;
}, $object);
}
$object = new Testclass();
// Output 1
print_r(chain_fun("addA()->addB()->get()", $object));
// Output 2
echo chain_fun("addA()->addB()->get()->a", $object);
?>
Output
akshay#db-3325:/tmp$ php test.php
stdClass Object
(
[a] => A
[b] => B
)
A
In my application i need many getter and setter and my idea was to generate them from an array, for example:
protected $methods = ['name', 'city'];
With this two parameters, i will need to generate the following methods:
public function getNameAttribute() {
return $this->getName();
}
public function getName($lang = null) {
return $this->getEntityValue('name', $lang);
}
And for city, the method will be:
public function getCityAttribute() {
return $this->getCity();
}
public function getCity($lang = null) {
return $this->getEntityValue('city', $lang);
}
Sure, i should need to generate the setter too (with the same logic).
As you can see, i will need a method with get<variable_name>Attribute and inside this call get<variable_name> and the other (getName) return even the same method (for each getter) and just change the 'name' parameter.
Every method have the same logic and i would like to generate them "dynamically". I don't know if this is possible..
You can leverage __call() to do this. I'm not going to provide a full implementation but you basically want to do something like:
public function __call($name, $args) {
// Match the name from the format "get<name>Attribute" and extract <name>.
// Assert that <name> is in the $methods array.
// Use <name> to call a function like $this->{'get' . $name}().
// 2nd Alternative:
// Match the name from the format "get<name>" and extract <name>.
// Assert that <name> is in the $methods array.
// Use <name> to call a function like $this->getEntityValue($name, $args[0]);
}
Send this params(name, city or other) as parameter to universal method(if you don't know what params you can get)
public function getAttribute($value) {
return $this->get($value);
}
public function get($value, $lang = null) {
return $this->getEntityValue($value, $lang);
}
If you know yours parameters, you can use this:
public function getNameAttribute() {
return $this->getName();
}
$value = 'Name'; //for example
$methodName = 'get' . $value . 'Attribute';
$this->$methodName; //call "getNameAttribute"
Take a look at this and let me know is it your requirement or not.
$methods = ['name', 'city'];
$func = 'get'.$methods[1].'Attribute';
echo $func($methods[1]);
function getNameAttribute($func_name){
$another_func = 'get'.$func_name;
echo 'Name: '.$another_func();
}
function getCityAttribute($func_name){
$another_func = 'get'.$func_name;
echo 'City: '.$another_func();
}
function getCity(){
return 'Dhaka';
}
function getName(){
return 'Frayne';
}
Can anyone please explain to me why the following code does not set the values on the array as expected? $_SESSION['foo'] stays empty, even after assigning time() and rand(). I've checked, the __get accessor method is actually called when assigning the variables but they aren't stored for one reason or another.
$test = Session::getSession('test');
$test->foo = array();
$test->foo[] = time();
$test->foo['baz'] = rand(1,9);
var_dump($_SESSION);
Using this simple Session wrapper
class Session
{
protected $namespace = null;
public static function getSession($namespace)
{
return new Session($namespace);
}
public static function destroySession($namespace)
{
if(isset($_SESSION[$namespace])) {
unset($_SESSION[$namespace]);
return true;
}
return false;
}
private function __construct($namespace)
{
$this->namespace = $namespace;
if(!isset($_SESSION[$namespace])) {
$_SESSION[$namespace] = null;
}
}
public function &__get($name)
{
return (isset($_SESSION[$this->namespace][$name])) ? $_SESSION[$this->namespace][$name] : null;
}
public function __set($name, $value)
{
$_SESSION[$this->namespace][$name] = $value;
}
}
In case it might be relevant, i'm using php 5.3.6
I 'm not sure if this can be made to work at all.
For one, to return by reference you should add the & operator at the call site as well. I 'm not sure how that might be possible without screwing up the nice syntax you 're trying to achieve.
Also, you cannot return expressions by reference (only variables). So this won't work:
public function &__get($name)
{
return (isset($_SESSION[$this->namespace][$name]))
? $_SESSION[$this->namespace][$name]
: null;
}
At the very least it should be written as
public function &__get($name)
{
$value = isset($_SESSION[$this->namespace][$name])
? $_SESSION[$this->namespace][$name]
: null;
return $value;
}
I'm building a little MVC system (learning) and I have some problems with showing variables in my view files.
This is from my View class:
private $vars = array();
public function __set($key, $value)
{
$this->vars[$key] = $value;
}
public function __get($key)
{
return $this->vars[$key];
}
public function __toString()
{
return $this->vars[$key];
}
public function show($file)
{
global $router;
$folder = strtolower($router->current_controller);
$path = VIEWPATH.$folder.'/'.$file.'.phtml';
if ( ! file_exists($path))
{
die("Template: $file, not found");
}
include ($path);
}
And here is from my controller:
$test = new View();
$test->name = 'karl';
$test->show('name_view');
And the view file (name_view)
echo $name // doesn't work
echo $this->name // Works
What am I doing wrong? Perhaps I haft to make something global?
THX / Tobias
EDIT: I just extracted the vars array in the view class right before I include the view file and then it worked.. Thank you for all help.
There is no $key in __toString()!
Also __toString() doesn't accept any parameters!
Test it with this:
public function __toString()
{
return json_encode($this->vars);
}
After your edit I realized that your problem is not on the __toString() method (you can just delete it since you're not using it). Doing echo $this->name is the correct way to show variables from inside your view in your case, however if you want to just do echo $name may I suggest a different approach?
function View($view)
{
if (is_file($view) === true)
{
$arguments = array_slice(func_get_args(), 1);
foreach ($arguments as $argument)
{
if (is_array($argument) === true)
{
extract($argument, EXTR_OVERWRITE);
}
}
require($view);
}
}
Use the View function like this:
$data = array
(
'name' => 'karl',
);
View('/path/to/your/name_view.phtml', $data);
Now it should work just by doing echo $name;, you can adapt it to your View class if you want to. If that doesn't work, try changing the name_view view extension to .php.