Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 770471
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T18:30:21+00:00 2026-05-14T18:30:21+00:00

Can PHP dissect its own syntax? For example, I’d like to write a function

  • 0

Can PHP dissect its own syntax? For example, I’d like to write a function that takes in an input like $object->attribute and says to itself:

OK, he’s giving me $foo->bar, which means he must think that $foo is an object that has a property called bar. Before I try accessing bar and potentially get a ‘Trying to get property of non-object’ error, let me check whether $foo is even an object.

The end goal is to echo a value if it is set, and fail silently if not.

I want to avoid repetition like this:

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

…and to avoid writing a function that does the above, but has to have the object and attribute passed in separately, like this:

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

…but to instead write something which:

  • preserves the object->attribute syntax
  • 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 this all depends on PHP being able to tell me "what kind of thing am I asking for when I say $object->attribute or $array[$key]", so that my function can handle each according to its own type.

Is this possible?

Update

I got some good answers here and did some experimenting. Wanted to summarize.

  • The answer to my original question appears to be "no," but we did find a way to accomplish was I was trying to do. Techpriester pointed out that I could pass the string ‘$foo->bar’ to a function and have it parse that and eval() it. Not the approach I want to take, but deserves a mention.
  • Peter Bailey pointed out that something like $foo->bar gets evaluated before getting passed to a function. I should have thought of this, but for some reason didn’t. Thanks, Peter!
  • Will Vousden showed that passing $foo->$bar by reference allows the function to evaluate it, rather than having it evaluated beforehand. His function shows that isset($foo->bar) will not complain if $foo is not an object, which I didn’t know.

More interestingly, his example seems to imply that PHP waits to evaluate a variable until it absolutely has to.

If you’re passing something by value, then of course PHP has to determine the value right then. Like this:

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

But if you’re passing by reference, it can wait to determine the value when it actually needs to.

Like this (following the order in which things happen):

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's evaluated - when we need it
    return $bar;
  }
}

Thanks to everyone who responded! If I’ve misstated something, please let me know.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T18:30:21+00:00Added an answer on May 14, 2026 at 6:30 pm

    You can pass it by reference:

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

    Note that this won’t work if you try to access an object as an array or vice versa.

    You shouldn’t try to rely on something like this too much, though.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 381k
  • Answers 381k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer ScrollIntoView works well for me. You can also try tree1.FocusedNode… May 14, 2026 at 10:04 pm
  • Editorial Team
    Editorial Team added an answer Ok, it ended up being something simple that took me… May 14, 2026 at 10:04 pm
  • Editorial Team
    Editorial Team added an answer The actual cause turned out to be a silly mistake… May 14, 2026 at 10:04 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.