Can PHP parse native syntax?

Can PHP parse native syntax? For example, I would like to write a function that takes an input, such as $object->attribute , and tells itself:

OK, he gives me $foo->bar , which means that he should think that $foo is an object that has the bar property. Before trying to access bar and potentially get the message “Trying to get a non-object property”, let me check if $foo even an object.

The ultimate goal is to echo the value if it is set, and fails if not .

I want to avoid repeating as follows:

 <input value="<? if(is_object($foo) && is_set($foo->bar)){ echo $foo->bar; }?> "/> 

... and to avoid writing a function that does the above, but must have an object and attribute passed separately, for example:

 <input value="<? echoAttribute($foo,'bar') ?>" /> 

... but instead write something that:

  • saves the syntax of the attribute object->
  • is flexible: can also handle array keys or regular variables

Like this:

 <input value="<? echoIfSet($foo->bar); ?> /> <input value="<? echoIfSet($baz['buzz']); ?> /> <input value="<? echoIfSet($moo); ?> /> 

But it all depends on how PHP can tell me "what I ask when I say $object->attribute or $array[$key] ", so that my function can handle each in its own type.

Is it possible?

Update

Here I got some good answers and experimented a bit. I wanted to take stock.

  • The answer to my original question seems to be no, but we found a way to do this, I tried to do it. Techpriester pointed out that I can pass the string '$ foo-> bar' to the function and parse it and eval() . Not the approach I want to take, but deserves a mention.
  • Peter Bailey noted that something like $foo->bar is evaluated before passing the function. I should have thought about it, but for some reason I didn’t. Thanks Peter!
  • Will Vousden showed that passing $foo->$bar by reference allows the function to evaluate it, rather than evaluate it in advance. Its function shows that isset($foo->bar) will not complain if $foo not an object that I did not know about.

More interestingly, his example seems to imply that PHP is waiting to evaluate a variable until it is absolutely required.

If you pass something by value , then of course PHP must define the value . Like this:

 $foo->bar = 'hi'; somefunction($foo->bar); // same as somefunction('hi'); 

But if you pass by reference , it can wait to determine the value when it is really needed.

Like this (following the order in which events occur):

 echo ifSet($foo->bar); // PHP has not yet evaluated $foo->bar... function ifSet(&$somevar){ // ...because we're passing by reference (&$somevar) // Now we're inside the function, but it still hasn't evaluated $foo->bar; it // just made its local variable, $somevar, point to the same thing as $foo->bar if(isset($somevar)){ // Right HERE is where it evaluated - when we need it return $bar; } } 

Thanks to all who responded! If I distorted something, let me know.

+4
source share
7 answers

You can pass it by the link:

 <?php function foo(&$bar) { if (isset($bar)) { echo "$bar\r\n"; } else { echo "It not set!\r\n"; } } $baz = new stdClass; $baz->test = 'test'; foo($baz->test); foo($baz->test2); $baz = array(); foo($baz['test3']); ?> 

Note that this will not work if you try to access the object as an array or vice versa.

You should not rely too much on something like that too.

+6
source

perhaps in this particular case you can use the @ symbol:

 <input value="<?php echo @$foo->bar; ?> "/> 
+3
source

The reason you can never do something like this

 echoIfSet($foo->bar); 

Because $foo->bar is evaluated before it is sent to the function. If this value is a string, all echoIfSet() will see the string - it does not know that the string is obtained from the object property.

So no , in a word. PHP cannot "parse its own syntax"

Every option you have (using __get () / __ set () or Reflection) involves more code and work than what you are trying to avoid - and they still don't give you exactly what you want .

+2
source

Wrapping your array and objects in a special array. you can use properties using php magic methods.

 class wrap implements ArrayAccess{ private $fields=array(); function __get($name){ if(isset($this->fields[$name])){ return $this->fields[$name]; } } function __set($name, $value){ $this->fields[$name] = $value; } // with function isset() public function offsetExists($offset) { return isset($this->$fields[$offset]); } public function offsetGet($offset) { return $this->$fields[$offset]; } public function offsetSet($offset, $value) { return $this->$fields[$offset] = $value; } public function offsetUnset($offset) { unset($this->$fields[$offset]); } } 

This will allow you to do whatever you need.

+2
source

There is a language function that will execute any PHP code that you pass to it as a string: eval() .

But you really, really (like in "I'm Dead Seriously") should not use it. This causes far more problems than it solves and makes your code indispensable.

Generally, a better and cleaner way.

"eval ()" leads to the dark side of the force, I mean PHP.

If you just need the "obj" part and the "attribute" of the supplied string, a regular expression can do this. It will be something like "/\$(\w+)->(\w+)/" . You can pass this to preg_match() and it will provide you with string components.

+1
source

Are you just looking for an eval instruction?

0
source

If you are looking for something that gives you the name of the variable you were passing in, for example:

 function foo($variable) { echo variableName($variable); } $test = "Hello World"; // echoes "test" foo($test); 

Then you will not find it. PHP does not have this capability.

0
source

Source: https://habr.com/ru/post/1308668/


All Articles