I'm trying to make the following example works. It looks like PHP thinks $this->getData2 as a member variable. How do I make it so that PHP thinks it as a method?
class Test {
public function getData()
{
return array(
'data1'=>array('name'=>'david'),
'data2'=>$this->getData2
);
}
public function getData2()
{
return "hello"
}
}
$test = new Test;
$data = $test->getData();
$data = $data['data2']();
I've tried the following, but looks like..I can't use $this in this case
function() use($this) {
return $This->getData2();
}
class Test {
public function getData(){
return array(
'data1'=>array('name'=>'david'),
'data2'=>'getData2'
);
}
public function getData2() {
return "hello";
}
}
$test = new Test;
$data = $test->getData();
$data = $test->$data['data2']();
echo $data;
Wasn't working without the $test-> on the $data = $test->$data['data2'](); line
And because I love fiddles: http://phpfiddle.org/main/code/4f5-v37
A callable to a method is an array with the object as a first member, and the method name as a second one.
So:
class Test {
public function getData()
{
return array(
'data1'=>array('name'=>'david'),
'data2'=>array($this, 'getData2')
);
}
public function getData2()
{
return "hello";
}
}
$test = new Test;
$data = $test->getData();
$data = $data['data2']();
Try:
class Test {
public function getData(){
return array('data1' => array('name' => 'david'), 'data2' => 'getData2');
}
public function getData2(){
return 'hello';
}
}
$test = new Test; $data = $test->getData(); echo $test->$data['data2']();
Easiest way would just be to do the calculation in a variable outside of the array and then just put the variable into the array
Related
I don't know what is this call system.
For example:
$a = SameClass::fnc1()->fnc2('input')->fnc2();
and we can see this method in jQuery:
$("#selector").parent().css('left',5).fadeOut(1500);
I don't have any idea to write this codes structure in PHP.
You just need to return object from a function
For example
class First
{
public function funcFirst()
{
$obj = new Second;
return $obj;
}
}
class Second
{
public function funcSecond($message)
{
return $message;
}
}
$first = new First;
$message = "Hello";
$result = $first->funcFirst()->funcSecond($message); // 'Hello'
I've got some problem.
I want to call static method of class from another class.
Class name and method are created dynamically.
It's not really hard to do like:
$class = 'className';
$method = 'method';
$data = $class::$method();
BUT, i want to to do it like this
class abc {
static public function action() {
//some code
}
}
class xyz {
protected $method = 'action';
protected $class = 'abc';
public function test(){
$data = $this->class::$this->method();
}
}
And it doesn't work if i don't assign $this->class to a $class variable, and $this->method to a $method variable.
What's the problem?
In PHP 7.0 you can use the code like this:
<?php
class abc {
static public function action() {
return "Hey";
}
}
class xyz {
protected $method = 'action';
protected $class = 'abc';
public function test(){
$data = $this->class::{$this->method}();
echo $data;
}
}
$xyz = new xyz();
$xyz->test();
For PHP 5.6 and lower you can use the call_user_func function:
<?php
class abc {
static public function action() {
return "Hey";
}
}
class xyz {
protected $method = 'action';
protected $class = 'abc';
public function test(){
$data = call_user_func([
$this->class,
$this->method
]);
echo $data;
}
}
$xyz = new xyz();
$xyz->test();
The object syntax $this->class, $this->method makes it ambiguous to the parser when combined with :: in a static call. I've tried every combination of variable functions/string interpolation such as {$this->class}::{$this->method}(), etc... with no success. So assigning to a local variable is the only way, or call like this:
$data = call_user_func(array($this->class, $this->method));
$data = call_user_func([$this->class, $this->method]);
$data = call_user_func("{$this->class}::{$this->method}");
If you need to pass arguments use call_user_func_array().
I was wondering how we can use the call_user_func_array() safely in the code.
Following coded function way is safe ?
function outertext() {
// …
if ($this->dom && $this->dom->callback!==null) {
call_user_func_array($this->dom->callback, array($this));
}
// …
}
What is best possible use of the call_user_func_array() of PHP. how we can use this function safely
Proof of concept: (how attacker can attack on this function)
<?php
class simple_html_dom_node {
private $dom;
public function __construct() {
$callback = array(new WP_Screen(), 'render_screen_meta');
$this->dom = (object) array('callback' => $callback);
}
}
class WP_Screen {
private $_help_tabs;
public $action;
function __construct() {
$count = array('count' => 'echo "schwag" > /tmp/1337h4x0rs');
$this->action = (object) $count;
$this->_help_tabs = array(array(
'callback' => 'wp_generate_tag_cloud',
'topic_count_scale_callback' => 'shell_exec'));
}
}
echo serialize(new simple_html_dom_node()).'𝌆';
?>
Check this modified example
<?php
class WP_Screen {
private $_help_tabs;
public $action;
function __construct() {
$count = array('count' => 'echo "schwag" > /tmp/1337h4x0rs');
$this->action = (object) $count;
$this->_help_tabs = array(array(
'callback' => 'wp_generate_tag_cloud',
'topic_count_scale_callback' => 'shell_exec'));
}
public function render_screen_meta()
{
echo __METHOD__;
}
}
class simple_html_dom_node
{
private $dom;
public function __construct()
{
$callback = array(new WP_Screen(), 'render_screen_meta');
$this->dom = (object) array('callback'=>$callback);
}
public function outer_text()
{
//verify the dom callback function here
if(is_callable($this->dom->callback))
{
//invoke the method here
call_user_func_array($this->dom->callback, array());
}
}
}
//create an object
$obj = new simple_html_dom_node();
//invoke the method
$obj->outer_text();
checkout the following example
<?php
function foobar($arg, $arg2) {
echo __FUNCTION__, " got $arg and $arg2\n";
}
class foo {
function bar($arg, $arg2) {
echo __METHOD__, " got $arg and $arg2\n";
}
}
// Call the foobar() function with 2 arguments
call_user_func_array("foobar", array("one", "two"));
// Call the $foo->bar() method with 2 arguments
$foo = new foo;
call_user_func_array(array($foo, "bar"), array("three", "four"));
?>
Output would be
foobar got one and two
foo::bar got three and four
validate first the method exist in the class or not using any of the function
method_exists or is_callable
Reference:
http://in2.php.net/manual/en/function.is-callable.php
http://in2.php.net/method_exists
Example:
<?php
class someClass {
function someMethod()
{
}
}
$anObject = new someClass();
$methodVariable = array($anObject, 'someMethod');
is_callable($methodVariable, true, $callable_name);
if($callable_name)
{
//use your function call here
call_user_func_array(callback_function, array(object));
}
I want to have a function and then use it multiple times with different parameters.
For example:
<?php
class Test {
var $test;
public function func($val) {
$this->test = $val;
}
public function buildFunc() {
if(!empty($this->test)) {
$ret = $this->test;
}
return $ret;
}
}
?>
Then on calling page:
$test = new Test;
$test->func("test1");
$test->func("test2");
echo $test->buildFunc();
Then it prints test2 on the screen. And I want it to print out both of them.
Either create 2 instances of your object;
$test1 = new Test;
$test1->func("test1");
$test2 = new Test;
$test2->func("test2");
echo $test1->buildFunc();
echo $test2->buildFunc();
Or make test an array;
class Test {
var $test = array();
public function func($val) {
$this->test[] = $val;
}
public function buildFunc() {
return print_r($this->test, true);
}
}
May be you mean that you want to store all values? Then use an array:
public function func($val) {
$this->test[] = $val;
}
public function buildFunc() {
return $this->test
}
And then work with the result as with an array.
Well.. your code does exactly what are you telling it to do. Consider situation when you have no OOP:
$str = 'test 1';
$str = 'test 2';
echo $str; //prints test 2
So you need to echo them separately as if it wont be an OOP situation.
$test = new Test;
$test->func("test1");
echo $test->buildFunc();
$test->func("test2");
echo $test->buildFunc();
When calling the method create 2 instances of the test object.
$test = new Test;
$test->func("test1");
echo $test->buildFunc();
$test2 = new Test;
$test2->func("test2");
echo $test2->buildFunc();
if you dont want to create 2 instances you have to make a array instead.
How about create a constructor and initialize the value of test and concat the second value.
<?php
class Test {
var $test;
public function __construct($init){
$this->test = $init;
}
public function func($val) {
$this->test .= $val;
return $this;
}
public function buildFunc() {
if(!empty($this->test)) {
$ret = $this->test;
}
return $ret;
}
}
$test = new Test("test1");
$test->func("test2");
echo $test->buildFunc();
?>
When you say both do you mean something like
test1test2
or do you want
test1
test2
For the first option you can just append the string:
<?php
class Test {
var $test;
public function func($val) {
$this->test = $test . $val; <-- add val to the end
}
public function buildFunc() {
if(!empty($this->test)) {
$ret = $this->test;
}
return $ret;
}
}
?>
For the second:
<?php
class Test {
var $test = array();
public function func($val) {
$this->test[] = $val; <-- add val to
}
public function buildFunc() {
if(!empty($this->test)) {
foreach($test as $item){
echo $item . "<br/>";
}
}
}
}
?>
Push the variables to an array
<?php
class Test {
var $test;
public function __construct(){
$this->test=array();//Declare $test as an array
}
public function func($val) {
$this->test[]=$val;//Push to array
}
public function buildFunc() {
if(!empty($this->test)) {
$ret = implode(",",$this->test);
}
return $ret;
}
}
?>
Need load parameters to function, creating a variable name on the fly terrible thing, but I do not see another solution. This code contains an error please help
<?php
class A{
public $vars;
public $tab_names;
public $tab_names = array('car'=>'audi', 'honda' => 'name');
public $tab_fruits = array('name'=>'banana', 'banana'=>'fruit');
public function load($varr){
$$varr;
$this->vars = $varr;
}
public function display(){
return $this->vars;
}
}
$ob = new A;
$ob->load('tab_names');
$ob->display();
?>
Like this?
public function load($varr){
$this->vars = $this->$varr;
}
You should create separate methods to return the disparate types of data. They can be wrappers for the "real" function, if necessary:
public function displayCars() {
return $this->tab_names;
}
public function displayFruit() {
return $this->tab_fruits;
}
This will obviate the need for variable variables.
I don't think you can set the value of an variable at the start
public $tab_names = array('car'=>'audi', 'honda' => 'name');
public $tab_fruits = array('name'=>'banana', 'banana'=>'fruit');
which should be
public $tab_names;
public $tab_fruits;
then
class A {
public $vars;
public $variables;
public $tab_names;
public $tab_fruits;
public function __construct($variables){
$this->variables = $variables;
}
public function load($varr){
$this->vars = $this->variables[$varr];
}
public function display(){
return $this->vars;
}
}
$variables = array();
$variables['tab_names'] = array('car'=>'audi', 'honda' => 'name');
$variables['tab_fruits'] = array('name'=>'banana', 'banana'=>'fruit');
$ob = new A($variables);
$ob->load('tab_names');
print_r($ob->display());
This is how I would do it:
class A {
public $loaded_vars;
public static $vars = array(
'tab_names' => array('car'=>'audi', 'honda' => 'name'),
'tab_fruits' => array('name'=>'banana', 'banana'=>'fruit')
);
public function load( $name ){
$this->loaded_vars = self::$vars[ $name ];
}
public function display() {
return $this->loaded_vars;
}
}
$ob = new A;
$ob->load( 'tab_names' );
$ob->display();