New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fatal error: Using $this when not in object context in \lib\Preprocessor.php(40) : eval()'d code on line 1 #1

Closed
compojoom opened this Issue Mar 27, 2011 · 5 comments

Comments

Projects
None yet
2 participants
@compojoom

compojoom commented Mar 27, 2011

Ok, I cannot get this to work. I'm trying to override a static class as described in the documenation:

Patchwork\replace("A::__toString", function()
{
return "redefined";
});

But I only get:
Fatal error: Using $this when not in object context in \lib\Preprocessor.php(40) : eval()'d code on line 1

I'm on php 5.3.1

@antecedent

This comment has been minimized.

Show comment
Hide comment
@antecedent

antecedent Apr 2, 2011

Owner

I'm afraid I won't be able to identify the problem without more context. For example, if it's a static class you're trying to override, then why does it have a __toString() method? Also, at what point exactly does the error occur? Is it thrown during redefinition, or only when the redefined method is called?

It would be ideal to see the source of the static class in question, but if that's impossible, answering the above questions should also shed some light on this issue.

Thank you.

Owner

antecedent commented Apr 2, 2011

I'm afraid I won't be able to identify the problem without more context. For example, if it's a static class you're trying to override, then why does it have a __toString() method? Also, at what point exactly does the error occur? Is it thrown during redefinition, or only when the redefined method is called?

It would be ideal to see the source of the static class in question, but if that's impossible, answering the above questions should also shed some light on this issue.

Thank you.

@compojoom

This comment has been minimized.

Show comment
Hide comment
@compojoom

compojoom Apr 2, 2011

Hey antecedent,
I just copied the code from your examples page:
http://antecedent.github.com/patchwork/docs/examples.html
Methods and Inheritance

I'm not trying to override a tostring method. Actually I try to override a method of joomla's JFactory class.

Here is a small part of the class:

class JFactory
{

/**
 * Get a mailer object
 *
 * Returns a reference to the global {@link JMail} object, only creating it
 * if it doesn't already exist
 *
 * @access public
 * @return object JMail
 */
function &getMailer( )
{
    static $instance;

    if ( ! is_object($instance) ) {
        $instance = JFactory::_createMailer();
    }

    // Create a copy of this object - do not return the original because it may be used several times
    // PHP4 copies objects by value whereas PHP5 copies by reference
    $copy   = (PHP_VERSION < 5) ? $instance : clone($instance);

    return $copy;
}

}

Normally you call the method with JFactory::getMailer(). So I tried to override it with:

Patchwork\replace("JFactory::getMailer", function()
{
return "redefined";
});

I'll have to trace when the error occurs, but I think that it is at redefinition time.

compojoom commented Apr 2, 2011

Hey antecedent,
I just copied the code from your examples page:
http://antecedent.github.com/patchwork/docs/examples.html
Methods and Inheritance

I'm not trying to override a tostring method. Actually I try to override a method of joomla's JFactory class.

Here is a small part of the class:

class JFactory
{

/**
 * Get a mailer object
 *
 * Returns a reference to the global {@link JMail} object, only creating it
 * if it doesn't already exist
 *
 * @access public
 * @return object JMail
 */
function &getMailer( )
{
    static $instance;

    if ( ! is_object($instance) ) {
        $instance = JFactory::_createMailer();
    }

    // Create a copy of this object - do not return the original because it may be used several times
    // PHP4 copies objects by value whereas PHP5 copies by reference
    $copy   = (PHP_VERSION < 5) ? $instance : clone($instance);

    return $copy;
}

}

Normally you call the method with JFactory::getMailer(). So I tried to override it with:

Patchwork\replace("JFactory::getMailer", function()
{
return "redefined";
});

I'll have to trace when the error occurs, but I think that it is at redefinition time.

@antecedent

This comment has been minimized.

Show comment
Hide comment
@antecedent

antecedent Apr 2, 2011

Owner

This should now work with the 1.1.1 tag. Thanks for the report!

Owner

antecedent commented Apr 2, 2011

This should now work with the 1.1.1 tag. Thanks for the report!

@antecedent antecedent closed this Apr 2, 2011

@compojoom

This comment has been minimized.

Show comment
Hide comment
@compojoom

compojoom Apr 2, 2011

That is working! Thank you very much!

I have another question - do you recommend using the library on production web sites?

Is it going to slow them a lot or the difference in speed is not noticeable?

compojoom commented Apr 2, 2011

That is working! Thank you very much!

I have another question - do you recommend using the library on production web sites?

Is it going to slow them a lot or the difference in speed is not noticeable?

@antecedent

This comment has been minimized.

Show comment
Hide comment
@antecedent

antecedent Apr 2, 2011

Owner

You're welcome.

And as for the question, Patchwork was designed with development/testing environments in mind. Consequently, issues like performance and possible security concerns weren't taken that seriously. This doesn't necessarily mean that it will make your site unbearably slow and vulnerable, but there are absolutely no guarantees about that. The performance hit may vary wildly from site to site, so it's recommended that you measure it yourself. As for security, no actual concerns come to my mind, but given the terribly hackish preprocessing mechanism in Patchwork, you may never know.

So, while you can definitely use Patchwork wherever you want, I wouldn't really recommend using it in production.

Owner

antecedent commented Apr 2, 2011

You're welcome.

And as for the question, Patchwork was designed with development/testing environments in mind. Consequently, issues like performance and possible security concerns weren't taken that seriously. This doesn't necessarily mean that it will make your site unbearably slow and vulnerable, but there are absolutely no guarantees about that. The performance hit may vary wildly from site to site, so it's recommended that you measure it yourself. As for security, no actual concerns come to my mind, but given the terribly hackish preprocessing mechanism in Patchwork, you may never know.

So, while you can definitely use Patchwork wherever you want, I wouldn't really recommend using it in production.

antecedent pushed a commit that referenced this issue Dec 21, 2017

Merge pull request #1 from APCOvernight/computamike-patch-1
issue with checking for incorrect int value
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment