This question already has answers here:
Enumerations on PHP
(39 answers)
Closed 2 years ago.
Does anyone know for php AbstractEnumeration if there is any way to do another level underneath it?
so like...
const a = 'a';
const b = 'b';
But I have an optional parameter for a:
const a = 'a' => '=123'
I know this is probably going to end up as a hash table instead, but just wondering what interesting things I can do with php enums.
PHP doesn't support native Enumerations.
You do something like:
abstract class ErrorCode
{
const NOT_FOUND = 404;
const OK = 200;
// etc.
}
$error = ErrorCode::NOT_FOUND;
This won't work in PHP:
const a = 'a' => '=123'
you could serialize the object as an array:
# serialize data into an array
define ("a", serialize (array ("a" => 123)));
# use it wherever you want
$a = unserialize (a);
Related
This question already has an answer here:
How to get the string name of the argument's type hint?
(1 answer)
Closed 2 years ago.
I have method which cast array to object by using
$class = get_class($object);
$methodList = get_class_methods($class);
But now I need had information about expected type of variable too. For example from this method:
public function setFoo(int $foo)
{
}
I need get int too. There is any option to get it?
You can use Reflection. Specifically ReflectionParameter::getType().
function someFunction(int $param, $param2) {}
$reflectionFunc = new ReflectionFunction('someFunction');
$reflectionParams = $reflectionFunc->getParameters();
$reflectionType1 = $reflectionParams[0]->getType();
$reflectionType2 = $reflectionParams[1]->getType();
assert($reflectionType1 instanceof ReflectionNamedType);
echo $reflectionType1->getName(), PHP_EOL;
var_dump($reflectionType2);
The above example will output:
int
NULL
This question already has answers here:
How do I dynamically write a PHP object property name?
(5 answers)
Closed 7 years ago.
This is not only tricky to explain, but tricky to do:
I'm trying to access and replace
$myObject->customField[0] = "some value";
but if I do
$str = "customField";
$myObject->$str[0] = "some value";
That doesn't work and if I do
$str = "customField";
$obj = $myObject->$str;
$obj[0];
That won't work either. I can change the values if I don't do this dynamically but I'm having to loop through a lot so doing it dynamic will be very helpful.
EDIT (answer)
Turns out curly braces does the trick. ie
$str = "customField";
$myObject->{$str}[0] = "some value";
Why do you want to have dynamic property names? The best answer is: don't do it this way. Consider using an associative array instead:
$myObject->customFields = array();
$myObject->customFields[$str] = "some value";
This question already has answers here:
Dynamically create PHP object based on string
(5 answers)
Closed 8 years ago.
I have a bunch of functions depending on a variable, I want to be able to do something like this:
(It returns an error hence the problem I'm unable to solve)
function($x) {
fetch.$x.() // if x=Name I would like it to execute fetchName()...and so on
}
and something like this
function($x) {
$article = new \Cc\WebBundle\Entity\$X();
// if x='name' to be executed Cc\WebBundle\Entity\name()
}
Sure, you could do that:
$basename = "fetch";
$key = ...; // your logic for generating the rest of function's name
$functionName = $basename . $key;
$functionName(); // execute function
Now, the tricky part would be if functions contain arbitrary set of arguments. In that case you should use call_user_func_array (docs).
As for creating of objects, meagar explained here please clear how to achieve that.
P.S. This, in fact, has very little to do with Symfony2. This is a pure PHP question ;)
Personally, I use the handy call_user_func_array() function like this:
<?php
$class = 'MyClassName';
$method = 'someMethod';
$parameters = array('foo', 'bar', 'baz');
call_user_func_array(array($class, $method), $parameters);
I imagine you would need to escape back-slashes in any name-spaced class names though, i.e. MCB\\MyClassName.
This question already has answers here:
How do I immediately execute an anonymous function in PHP?
(9 answers)
Closed 10 years ago.
Is it possible to do something like this. Lets say we have a function that accepts string as argument. But to provide this string we have to do some processing of data. So I decided to use closures, much like in JS:
function i_accept_str($str) {
// do something with str
}
$someOutsideScopeVar = array(1,2,3);
i_accept_str((function() {
// do stuff with the $someOutsideScopeVar
$result = implode(',', $someOutsideScopeVar); // this is silly example
return $result;
})());
The idea is to when calling i_accept_str() to be able to directly supply it string result... I probably can do it with call_user_func which is known to be ineffective but are there alternatives?
Both PHP 5.3 and PHP 5.4 solutions are accepted (the above wanted behavior is tested and does not work on PHP 5.3, might work on PHP 5.4 though...).
In PHP (>=5.3.0, tested with 5.4.6) you have to use call_user_func and import Variables from the outer Scope with use.
<?php
function i_accept_str($str) {
// do something with str
echo $str;
}
$someOutsideScopeVar = array(1,2,3);
i_accept_str(call_user_func(function() use ($someOutsideScopeVar) {
// do stuff with the $someOutsideScopeVar
$result = implode(',', $someOutsideScopeVar); // this is silly example
return $result;
}));
This question already has answers here:
How do I access this object property with an illegal name?
(2 answers)
Closed 9 years ago.
The json format.
{
"message-count":"1",
"messages":[
{
"status":"returnCode",
"error-text":"error-message"
}
]
}
In php, I successfully get "status" value with $response->messages[0]->status
But when I wanted to access "error-text" properties, the code $response->messages[0]->error-text gives me error.
How to access object properties with hyphen?
here is the way!
$object->{"message-count"};
$response->messages[0]->{'error-text'};
hope this helps
any string (bytes sequence) can be used as a class field
$object->{"123"} = 10; // numbers
$object->{"{a}"} = 10; // special characters
$object->{"òòèè"} = 10; // non ascii characters
Use the {} syntax:
echo $response->messages[0]->{'error-text'};
Please, use standard PHP feature - accessing variables within curly braces:
class t {}
$a = new t();
$a->{"o-o"} = 1;
echo $a->{"o-o"};
So, you need to write $response->messages[0]->{"error-text"}.