Is it possible to define functions under stdClass - php

Here i tried to define a function under stdClass object.
<?php
$x= function($name){return "Hello ".$name;};
echo $x("Sun");
$hey = (object)[ "x" => function($name){return "Hello ".$name;}, "y" =>"Hello Venus"];
echo $hey->x("Mercury");
echo $hey->y;
But it says: Fatal error: Call to undefined method stdClass::x()

This is the closest you can get:
$x = function ($name) { return 'hello ' . $name; };
$obj = new stdClass();
$obj->x = $x;
echo call_user_func($obj->x, 'john'); // this will work
echo $obj->x('john'); // this will not work
You won't need to use call_user_func with php7 though.

Related

Access specific parameter of a function

How could I access specific parameter from a function, example:
function someFunction()
{ echo $a = 7;
echo $b = 70;
}
someFunction();//770
How can I return only $a or $b, is possible ?
echo vs return
First of all, I think it is important to note that echo and return have very different behaviors and are used in very different contexts. echo simply outputs whatever is passed to it, either to an html page, or to the server log.
<?php
$a = 5;
function printFoo() {
echo 'foo';
}
echo '<h1>Hello World!</h1>'; // prints an h1 tag to the page
echo $a; // prints 5 to the page
foo(); // prints foo to the page
?>
return on the other hand is used to "[end] execution of the current function, and return its argument as the value of the function call." return can only ever take one argument. It can also only be executed once in a function; once it is reached, the code will jump out of the function back to where the function was invoked.
<?php
function getFoo() {
return 'foo';
}
// print the value returned by getFoo() directly
echo getFoo();
// store it in a variable to be used elsewhere
$foo = getFoo(); // $foo is now equal to the string 'foo'
function getFooBar() {
return 'foobar'; // code beyond this statement will not be executed
echo 'something something';
return 'another foobar';
}
echo getFooBar(); // prints 'foobar'
?>
function paramters
As it stands, someFunction can only return $a or $b or an array containing $a and $b, which may become a problem if, say, you need to print out $c. To make the function more reusable, you can pass it an argument and then reuse the function wherever you like.
<?php
function printSomething($myVar) {
echo $myVar;
}
$a = 7;
$b = 70;
$c = 770;
printSomething($a) . '\n';
printSomething($b) . '\n';
printSomething($c) . '\n';
printSomething(7000); // you don't have to pass it a variable!
// Output:
// 7
// 70
// 700
// 7000
?>
If you wish to return only one parameter then you just use the return statement.
<?php
function someFunction()
{
$a = 7;
$b = 70;
return [$a, $b];
}
$arrayS = someFunction();//array containing $a and $b
echo $arrayS[0]."\n";
echo $arrayS[1]."\n";
echo "\n";
echo "Another way to access variables\n";
echo someFunction()[0]."\n";
echo someFunction()[1]."\n";

Assigning Object as Value and Reference doesnt work Properly in PHP

I am from Java Background. I have used objects as Call by Value and Reference in Java.
But When I am using these things in PHP i didnt achieve the my expected Result.
<?php
class A {
public $t;
function __construct() {
$this->t = 100;
}
}
/*By value */
echo 'By Value<br/>';
$obj1 = new A();
echo $obj1->t;
$obj2 = $obj1;
$obj2->t = 200;
echo $obj2->t;
echo $obj1->t; //expects Result 100 but it prints 200
/*By Reference */
echo '<br/>By Reference<br/>';
$obj3 = new A();
echo $obj3->t;
$obj4 = &$obj3;
echo $obj4->t;
$obj4->t = 500;
echo $obj4->t;
echo $obj3->t;
?>
When I am assigning one object to another one Object by value, If i modify the value of Property in Copied Object means, this will be affected Original Object too.
Plz Clear my doubt anyone...
Thanks...
Humm...PHP support both By Value and By Reference. However, there are a few quirks...
By default, PHP5 always assigns / passes objects by reference:
$obj1 = new stdClass();
$obj2 = $obj1;
print spl_object_hash($obj1);
print "<br>";
print spl_object_hash($obj2);
Output:
000000001ef37c150000000046e30ead
000000001ef37c150000000046e30ead
Basically, both $obj1 and $obj2 are pointers to the same object.
However, this is not true for primitive types (such as integer, string, etc...)
$var1 = "some string";
$var2 = $var1;
$var1 = "foo";
print $var1;
print '<br>';
print $var2;
Output:
foo
some string
Using & you can pass by reference primitives too.
$var1 = "some string";
$var2 = &$var1;
$var1 = "foo";
print $var1;
print '<br>';
print $var2;
Output:
foo
foo
And you can pass objects by value too
$obj1 = new stdClass();
$obj2 = clone $obj1;
print spl_object_hash($obj1);
print "<br>";
print spl_object_hash($obj2);
output:
0000000062516daa000000001745e928
0000000062516da9000000001745e928
In Short, by default primitive types are passed by value while objects are passed by reference.

PHP Object Indices as Strings?

Is there a way to access a member of an object, by using its name as a string?
When I declare an array...
$array = array();
$array['description_en']="hello";
$array['description_fr']="bonjour";
then I access a member like this:
$lang="en"; //just to show my purpose. it will be dynamic
$description = $array['description_'.$lang];
Can I do the same thing for objects?
For example:
$obj->description_en="hello";
$obj->description_fr="bonjour";
How can I access $obj->description_.$lang ?
class test
{
public $description_en = 'english';
}
$obj = new test();
$lang = 'en';
echo $obj->{"description_".$lang}; // echo's "english"
You can see more examples of variable variables here.
You can use this syntax:
<?php
class MyClass {
public $varA = 11;
public $varB = 22;
public $varC = 33;
}
$myObj = new MyClass();
echo $myObj->{"varA"} . "<br>";
echo $myObj->{"varB"} . "<br>";
echo $myObj->{"varC"} . "<br>";
This way, you can access object variables as if they were entries in an associative array.

How can you reference a object with a class variable?

I couldn't find a answer to my question and I am thinking there is something easy I am missing..
I am trying to reference a a value within a object with a variable in a class. In this case I want the line on the bottom:
echo $b->ref->$a->type
to output 'testing' like the following two will:
echo $b->ref->test; // outputs 'testing'
$c = $a->type;
echo $b->ref->$c; // outputs 'testing'
Full code:
<?php
class A {
public $type;
public function set_type($type) {
$this->type = $type;
}
}
class B {
public $ref;
public function set_reference($ref) {
$this->ref = $ref;
}
}
$a = new A();
$b = new B();
$b->set_reference( (object) array('test' => 'testing', 'test2' => 'testing2') );
$a->set_type('test');
echo $b->ref->test; // outputs 'testing'
echo '<br />';
echo $a->type; // outputs 'test'
echo '<br />';
$c = $a->type;
echo $b->ref->$c; // outputs 'testing'
echo '<br />';
echo $b->ref->$a->type; // error
What am I missing to be able to do this? Or, is this not possible?
Same as always.
echo $b->ref->{$a->type};
Did you tried this:
echo $b->ref->{$c};

PHP : anonymous function in associative array

Is is possible? Something like (which doesn't work) :
$prototype = array(
'ext' => function ($args)
{
$ext = NULL;
if (in_array(func_get_arg(0), array('js', 'css')))
return $ext;
else
return 'js';
},
);
Yes. The only limitation is that you can't cast it to an object.
<?php
$foo = array(
'bar' => function($text)
{
echo $text;
}
);
$foo['bar']('test'); //prints "test"
$obj = (object)$foo;
$obj->bar('test'); //Fatal error: Call to undefined method stdClass::bar() in /code/REGnPf on line 11
?>
It certainly is:
<?php
$array = array(
'func' => function($a) {
return $a + 2;
}
);
echo $array['func'](3);
?>
This will give you 5 =)!

Categories