I want to write like this in php. How can i express samely into php?
$test = '{"longUrl": "http://www.yahoo.com"}';
Thanks.
If you want to write actual PHP code to make a new object (assuming your example is JSON), there's no literal/shortcut syntax in PHP for that; you have to make a new stdClass object and set its variables manually:
$test = new stdClass;
$test->longUrl = "http://www.yahoo.com";
If you are comfortable writing JSON inside a string, as you are doing in your example, simply feed that into json_decode() and you have yourself a stdClass object:
$test = json_decode('{"longUrl": "http://www.yahoo.com"}');
$test = array("longUrl" => "http://www.yahoo.com");
>echo $test['longUrl']
http://www.yahoo.com
Related
I was reading a code example of something, and noticed a syntax that isn't familiar to me.
$response = $controller->{'home'}();
Is that a valid php syntax?
Yes.
$controller->{'home'}
// same as
$controller->home
// used to access a property
And
$controller->{'home'}()
// same as
$controller->home()
// used to call a method
The main benefit is that, by calling ->{stuff}, you can access properties with different (or strange) names.
Example:
$a = new stdClass();
$a->{'#4'} = 4;
print_r($a);
// stdClass Object
// (
// [#4] => 4
// )
You can't do $a->#4, but can do $a->{'#4'}
See this, for instance: https://3v4l.org/PaOF1
Yes it is. The cool thing about it is that you can call object's methods based on values stored in variables:
$whatToExecute = 'home';
$response = $controller->{$whatToExecute}();
Good luck!!
It know it can be done with get_class($variable).
The problem is that my $object is actually a string containing the variable name.
so:
$object = new MyClass();
$var = '$object';
$class = get_class($var); // obviously fails
I can't use get_class($object), because I don't have direct access to that variable (I'm producing the $var string from parsing a PHP expression using token_get_all())
I tried using eval(sprintf('return get_class(%s);', $var)), but it doesn't work because the variable appear undefined from eval's scope :(
Is there a way to do this?
I need to know the class in order to pass it to ReflectionMethod, so I can get information about a method (the next element in the PHP expression).
NVM: I'm pretty sure it is not possible. Sorry for asking:)
you can do
$var = new $object();
Try using variable variables: http://php.net/manual/en/language.variables.variable.php
Something like:
$var = 'object';
$class = get_class( $$var );
you can do the following
$ref = ltrim($var, '$');
get_class($ref);
I am trying to use PHP to build HTML that I will later display. I have the following lines:
$currentName = "test";
$numberToGiveDropDownHTML = "<Select name='$currentName[]'>\n";
But I get the following error:
Parse error: syntax error, unexpected
']', expecting T_STRING or T_VARIABLE
or T_NUM_STRING in...
What is wrong with that? I thought I could that and later use the $_POST super global.
Thanks!
PHP will try to parse variables in a double string automatically. Consider the following:
$a = 'foo';
echo "A = $a"; // output: A = foo
PHP will convert $a to foo for you, given it's in double quotes. In your scenario, it's thinking (the parser that is) that the [] is part of the in-line variable.
You can do one of two things: use {$currentname} so the parser doesn't keep going, or break the string out and concatenate the variable in, e.g. ...".$currentname."[]...
That brackets are referencing an array, but $currentName isn't an array.
Try this:
$currentName = "test";
$numberToGiveDropDownHTML = "<Select name='{$currentName}[]'>\n";
Or:
$currentName = "test";
$numberToGiveDropDownHTML = "<Select name='$currentName" . "[]'>\n";
Either should work...
Good luck!
I know you can create an array like this:
$a = array();
and append new name value pairs to it like thus:
$a['test'] = 'my new value';
It is even possible to omit the first line, although bad practice!
I find objects easier to read and understand, so what I've done is taken the array of name value pairs and cast it into an object:
$a = (object)$a;
Thus I can access the parameters:
$a->test;
It seems wasteful for the extra overhead of creating an Array to start with, is it possible to simply create an object and then somehow just add the name value pairs to it in a similar way as I would do the array?
Thanks
Yes, the stdClass class is designed for just that.
$a = new stdClass;
$a->test = 'my new value';
You can think of it as being akin to the following JavaScript code:
var a = {};
a.test = 'my new value';
In fact, if you had some PHP code that received data as JSON, running json_decode() on the data results in a stdClass object with the included properties.
You can use the stdClass object for that:
$a = new stdClass();
It is very simple even without stdclass. You can simply do
class obj{}
$obj = new obj;
$obj->foo = 'bar';
You can use stdClass for this.
$object = new StdClass;
$object->foo = 'bar';
A Google APIs encoded in JSON returned an object such as this
[updated] => stdClass Object
(
[$t] => 2010-08-18T19:17:42.026Z
)
Anyone knows how can I access the $t value?
$object->$t obviously returns
Notice: Undefined variable: t in /usr/local/...
Fatal error: Cannot access empty property in /....
Since the name of your property is the string '$t', you can access it like this:
echo $object->{'$t'};
Alternatively, you can put the name of the property in a variable and use it like this:
$property_name = '$t';
echo $object->$property_name;
You can see both of these in action on repl.it: https://repl.it/#jrunning/SpiritedTroubledWorkspace
Correct answer (also for PHP7) is:
$obj->{$field}
Have you tried:
$t = '$t'; // Single quotes are important.
$object->$t;
I'm using php7 and the following works fine for me:
class User {
public $name = 'john';
}
$u = new User();
$attr = 'name';
print $u->$attr;
this works on php 5 and 7
$props=get_object_vars($object);
echo $props[$t];