0

EDIT: I'm adding a screenshot of the actual errors, as a proof that this is what happens.

error message

Note, that the $post_id and $id properties are undefined, because the parent Y_CF class has a magic __get() method, but again - it's not available in the child class.


I've had a bunch of issues already(including one, where PHP would pretty much just ignore a portion of an included file, even if I threw a bunch of random characters in there, which should cause a fatal error), but I'm finally down to a single issue: I have two classes - a parent and a main class. The parent class is abstract and has a factory method that returns different child classes, based on parameters passed to it. The odd thing happens, with one of the child classes - it doesn't have the methods of the parent class. I'm posting only the relevant pieces of code

abstract class Y_CF {
    // even if "public", it doesn't work
    protected function parse_attributes( $attributes, $core_atts = array() ) {
        $this->__settings['attributes'] = wp_parse_args( $core_atts, $attributes );
    }
}

class Y_CF_Text extends Y_CF {
    public function __construct( $settings ) {
        parent::__construct( $settings );
        // This is false
        var_dump( method_exists( $this, 'parse_attributes' ) );

        $this->parse_attributes( $this->attributes, array(
            'id' => "Y_field_{$this->id}",
            'class' => esc_attr( 'yotta-text-input ' . $this->class ),
            'type' => ! empty( $this->attributes['type'] ) ? $this->attributes['type'] : 'text',
            'value' => $this->format_value( $this->value ),
            'name' => $this->gen_name(),
        ) );
    }
}

As you can see, my main class is Y_CF and I'm extending it with Y_CF_Text.

Note, that I've tried this code on my local server(ubuntu 14) and it works just fine(as it should).

I'm suspecting that the problem is somewhere in the server configuration, but I don't have any clue where.

What I did so far:

  • Updated PHP to 5.5.12 ( the issue was happening with 5.3 as well )
  • Disabled opcache
  • Disabled memcached
  • Restarted php, nginx and the whole server

All of those had no effect whatsoever.

I've also looked all over the internet and couldn't find anything that remotely mimics my situation.

Nikola Ivanov Nikolov
  • 5,022
  • 1
  • 26
  • 27
  • Does the parent class hace a constructor? If not, get rid of the parent::__construct – gontrollez May 23 '14 at 12:04
  • Yes it does - I skipped it for readability. Note, that I have almost identical classes, that don't have this issue(each class takes care of a different type of form input). – Nikola Ivanov Nikolov May 23 '14 at 12:05
  • Also the code **does work** on a different set-up, which is what leads me to think that it's a configuration issue(although, heck - what kind of configuration parameter disables class inheritance). – Nikola Ivanov Nikolov May 23 '14 at 12:08
  • I don't think that what you're describing is possible. It's probably a bug or otherwise confusing behavior of `method_exists()`. – Narf May 23 '14 at 12:41
  • @Narf - that's what I thought too, but it is a fact. I get a fatal error saying that the method doesn't exist for the Y_CF_Text class... – Nikola Ivanov Nikolov May 23 '14 at 12:44
  • @NikolaIvanovNikolov It's surely not a configuration issue, and since you've already upgraded PHP, it's most likely a typo then ... I'm sure you've already checked, but try typing the method name again in both places - who knows, one of the characters could be in cyrilic or something ... – Narf May 23 '14 at 12:49
  • @Narf - I zipped all of the files and downloaded them to my computer. I set-up the virtual host, added an entry to my hosts file and everything was working fine. All of that without changing a single file(except for the database configuration), so I'm **sure** this is not a typo. – Nikola Ivanov Nikolov May 23 '14 at 12:57
  • See my edit with the screenshot of the error as a proof that it really is not working. – Nikola Ivanov Nikolov May 23 '14 at 13:07
  • If it is a server configuration issue and at some point it "ignored" part of you code it sounds likely that you should see something in the error logs. Can you look there? Also, can you var_export the object you believe to be of the child class? – monocell May 23 '14 at 13:18
  • Are both of these classes in the same source file? – Philip Adler May 23 '14 at 13:20
  • @monocell - http://pastie.org/9202517 . There are references to other objects(a group object that holds all fields and a post object). I'll also look through the various logs on the server. Philip Adler - yes, they are in the same class. – Nikola Ivanov Nikolov May 23 '14 at 13:25
  • @NikolaIvanovNikolov Are you exporting the object at the same place as you are using it? i.e. where you get the undefined notices? – monocell May 23 '14 at 13:39
  • @monocell - that's correct. I do the export directly above the place where I get the fatal error). – Nikola Ivanov Nikolov May 23 '14 at 13:40
  • The only thing I can think of is that you have old code lying around and it includes a class of the same name as the one you are extending from, and that class lacks the method in question. Or you have another version of the child class or something like that. Can't think of anything else that might explain this. – monocell May 23 '14 at 13:49
  • I'm as stumped as you are... And your guesses shouldn't be the case, since I'm declaring the base class(I don't use `class_exists()` checks - I know, I'm bad for that) and I don't get a fatal error that the class already exists. Oh well, I'll keep trying and see what comes out of this... – Nikola Ivanov Nikolov May 23 '14 at 13:51
  • Well, it might still happen if the correct code is not included. Which would explain why you could insert random characters into it and still not get a fatal error. (I also don't use class_exists, I think it's a horrible idiom) – monocell May 23 '14 at 13:56
  • But again - I have the completely same code over on my computer. This file is part of a WordPress plugin, so I have all of the files that run both times and yet it works just fine on my computer :( I've never seen such a problem before and it's driving me crazy – Nikola Ivanov Nikolov May 23 '14 at 14:06
  • Yeah, so my guess was that you have all the same files on the server + a few more. Or that something causes the set of included files to be different between your server and locally. Regardless of what the problem turns out to be, It would be nice if you updated the question when you find it. I'm interested as well now. :/ – monocell May 23 '14 at 14:12
  • Figured it out finally - incorrectly opened php tag and the server was configured to not accept short tags. – Nikola Ivanov Nikolov May 23 '14 at 14:44
  • @NikolaIvanovNikolov: I think you should write this comment up with a little more detail as an answer, and accept it. – Mike Sherrill 'Cat Recall' May 24 '14 at 09:23
  • I tend to use the hammer towards this one: [Are PHP short tags acceptable to use?](http://stackoverflow.com/q/200640/367456) as it looks like a useful addition, however it is not an exact duplicate. The so far voted close reason looks fitting to me. – hakre May 24 '14 at 09:32
  • @hakre - Well, to me convenience in short tags is really not a reason to use them(I'm saying that regarding to some of the answers on that thread). Instead, using a good text editor with tab-triggered snippets is **so** much better :) And I usually don't use them and use either `php{tab}`, or write them myself. I guess I wrote that myself and forgot the rest of the syntax :) – Nikola Ivanov Nikolov May 24 '14 at 11:39
  • @NikolaIvanovNikolov: FYI: I put this to discussion here: http://meta.stackoverflow.com/q/256048/367456 – hakre May 24 '14 at 13:43

1 Answers1

1

Well, I feel very unlucky now :)

As a final resort, I started putting each of the classes in their own file. I got to the main class and I suddenly got the following error:

<b>Parse error</b>: syntax error, unexpected end of file in <b>/path/to/site/wp-content/plugins/yotta-util/lib/class-y-cf.php</b> on line <b>291</b><br />

I was like WHAT?! So, I started going through the whole file and I finally pin-pointed the issue to a single method of the class. When I scrolled to the end of the method, I saw this:

{some javascript code}
</script>
<?

Notice the missing "php" part after the question mark - that's where all of those issues were coming from. A single opening tag mistyped.

Now, I don't usually use the short tags, but I guess most servers are already configured to handle them properly, so I never got a single problem. But the server I'm working on right now has them turned off.

Somehow, instead of triggering a fatal error or anything at all, this only caused the described puzzling mess.

So in the end - it was just a single typo that caused me to bang my head around for at least 6 hours.

Nikola Ivanov Nikolov
  • 5,022
  • 1
  • 26
  • 27