E_STRICT error with ReflectionClass::newInstance()

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
georgeoc
Forum Contributor
Posts: 166
Joined: Wed Aug 09, 2006 4:21 pm
Location: London, UK

E_STRICT error with ReflectionClass::newInstance()

Post by georgeoc »

I'm getting the following errors when trying to use Chris Corbyn's Crafty Dependency Injection Container:

Code: Select all

Unexpected PHP error [Declaration of Crafty_ComponentReflector::newInstance() should be compatible with that of ReflectionClass::newInstance()] severity [2048]
 
Unexpected PHP error [Declaration of Crafty_ComponentReflector::newInstanceArgs() should be compatible with that of ReflectionClass::newInstanceArgs()] severity [2048]
I can get first error if I change the declaration of newInstance() to:

Code: Select all

public function newInstance($args)
but I can't work out which change is needed to make the newInstanceArgs() method compatible. Can't find any other cases of this error in Google, or a definition of newInstanceArgs() which seems to be incompatible with what I've already got.

Code: Select all

class Crafty_ComponentReflector extends ReflectionClass
{
  
  /**
   * Properties to be injected when instantiated.
   * @var mixed[]
   */
  private $_properties = array();
  
  /**
   * Create a new ComponentReflector for the given $className with the given
   * $properties.
   * @param string $className
   * @param mixed[] $properties
   */
  public function __construct($className, $properties = array())
  {
    parent::__construct($className);
    $this->_properties = $properties;
  }
  
  /**
   * Create an instance with a list of arguments passed to the constructor.
   * @param mixed $param1
   * @param mixed $param2...
   * @return object
   */
  public function newInstance()
  {
    return $this->newInstanceArgs(func_get_args());
  }
  
  /**
   * Create an instance with the given array of arguments in the constructor.
   * @param mixed[] $args
   * @return object
   */
  public function newInstanceArgs($args)
  {
    if ($this->getConstructor())
    {
      $o = parent::newInstanceArgs($args);
    }
    else
    {
      $o = parent::newInstance();
    }
    
    foreach ($this->_properties as $k => $v)
    {
      $setter = 'set' . ucfirst($k);
      $this->getMethod($setter)->invoke($o, $v);
    }
    
    return $o;
  }
  
}
 
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: E_STRICT error with ReflectionClass::newInstance()

Post by Chris Corbyn »

Whooaa... Will fix that asap :)

Indeed, I'm not sure what is up with the newInstanceArgs(). Surely the PHP implementation only uses one parameter.

I guess I should actually finish the website if people are using it :P I have a bunch of planned changes/optimizations.

EDIT | Before I start fixing this I'd just like to say this is really weird because I wasn't lying when I said it works under E_STRICT. I did at the time of writing :?
georgeoc
Forum Contributor
Posts: 166
Joined: Wed Aug 09, 2006 4:21 pm
Location: London, UK

Re: E_STRICT error with ReflectionClass::newInstance()

Post by georgeoc »

It's very strange. Do let me know if you find a solution.

I'm playing around with Sweety as well this evening. I love the Swiftmailer test suite - it's basically what I'd already cobbled together myself plus about 100% extra functionality! Having some errors with Javascript at the moment, but I'll post in a new thread if I can't solve them. I'm well aware neither Sweety nor Crafty are stable, and don't expect you to provide support.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: E_STRICT error with ReflectionClass::newInstance()

Post by Chris Corbyn »

Code: Select all

public function newInstance($args)
ReflectionClass::newInstance() doesn't have any required parameters. That's just weird.

And for some reason this little snippet doesn't give me any E_STRICT notices. It has the same method declarations as far as I know.

Code: Select all

<?php
 
error_reporting(E_ALL | E_STRICT);
ini_set('display_errors', true);
 
class MyReflectionClass extends ReflectionClass {
 
  public function __construct($className, $properties = array()) {
    parent::__construct($className);
  }
 
  public function newInstance() {
    return $this->newInstanceArgs(func_get_args());
  }
 
  public function newInstanceArgs(array $args) {
    return parent::newInstanceArgs($args);
  }
 
}
 
$o = new MyReflectionClass('stdClass');
var_dump($o->getMethods());
georgeoc
Forum Contributor
Posts: 166
Joined: Wed Aug 09, 2006 4:21 pm
Location: London, UK

Re: E_STRICT error with ReflectionClass::newInstance()

Post by georgeoc »

I get no error with that snippet either.

Can you recreate the error I experienced with the Crafty_ComponentReflector class? If not, I'll extrapolate upwards from the snippet and try and determine where the error comes from.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: E_STRICT error with ReflectionClass::newInstance()

Post by Chris Corbyn »

I think I'm just going to have to stop extending the ReflectionClass and use composition instead. I can't see a way around it. I tried analyzing its method declarations using "new ReflectionClass('ReflectionClass')" ;) But it throws exceptions about trying to determine default values for method parameters etc. Stupid PHP :P

It should be easy enough to just handle it like this:

Code: Select all

class Crafty_ComponentReflector {
  
  // ....
  
  private $_reflectionClass;
  
  public function newInstance() {
    return $this->_reflectionClass->newInstanceArgs(func_get_args());
  }
 
  // ...
 
 
}
Sweety is looking nice yeah. I wasn't aware of any JS errors though. To be honest, I spend most of my time running them with the CLI interface (php sweety/run.php [optional args]).

Could you PM me the JS error details please? :)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: E_STRICT error with ReflectionClass::newInstance()

Post by Chris Corbyn »

georgeoc wrote:I get no error with that snippet either.

Can you recreate the error I experienced with the Crafty_ComponentReflector class? If not, I'll extrapolate upwards from the snippet and try and determine where the error comes from.
I can recreate the error yeah.... I just updated the copy sweety in that project so it works in E_STRICT (copied it from Swift) and then ran the tests. Bam, those errors are there!
georgeoc
Forum Contributor
Posts: 166
Joined: Wed Aug 09, 2006 4:21 pm
Location: London, UK

Re: E_STRICT error with ReflectionClass::newInstance()

Post by georgeoc »

At least it's not just me then.

By the way, I just got Crafty working - it's fantastic! I have added accesskey tags to the 'Run Tests' button and the search box, for mouse-free testing! The only thing I'll have to get used to is not being able to print stuff to the screen as a quick-n-easy check when a test fails - I use a custom class instead of var_dump(), and its JavaScript makes the XML malformed! Do you have any tricks you use to echo test vars to the screen when you can't debug a problem?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: E_STRICT error with ReflectionClass::newInstance()

Post by Chris Corbyn »

georgeoc wrote:At least it's not just me then.

By the way, I just got Crafty working - it's fantastic! I have added accesskey tags to the 'Run Tests' button and the search box, for mouse-free testing! The only thing I'll have to get used to is not being able to print stuff to the screen as a quick-n-easy check when a test fails - I use a custom class instead of var_dump(), and its JavaScript makes the XML malformed! Do you have any tricks you use to echo test vars to the screen when you can't debug a problem?
Guess you mean Sweety ;) I'd love to see your changes.

I'm actually on the SimpleTest development team now so can take a look at some of these scenarios which cause the XML to be broken. I was already aware of a handful of places where the XML breaks (making a binary assertIdentical() for example dumps raw binary into the XML).

PS: Anywhere in your code you can do a debug dump() by calling:

Code: Select all

SimpleTest::getContext()->getTest()->dump($whatever);
That way it'll be handled by the test framework correctly. Any arbitrary output jut ends up in the produced XML right now which malforms it.
georgeoc
Forum Contributor
Posts: 166
Joined: Wed Aug 09, 2006 4:21 pm
Location: London, UK

Re: E_STRICT error with ReflectionClass::newInstance()

Post by georgeoc »

Chris Corbyn wrote:Guess you mean Sweety ;) I'd love to see your changes.
Sorry, Sweety. Or "Sorry sweety" if you prefer.

I've fiddled with the CSS a bit, but other than that just added a couple of accesskeys:

Code: Select all

         <div>
            <input type="text" class="sweety-text" id="sweety-filter" accesskey="F"
              onkeyup="sweetyFilter.search();" />
            <input type="submit" id="sweety-run-button" value="Run Tests" accesskey="S"
              onclick="sweetyUI.initialize(); sweetyRunner.runAll();" />
          </div>
 
The brilliant thing about doing this is that in both Safari and Firefox, using the Control-S access key to start the tests will leave the 'Run Tests' button highlighted, so that when you next return to the browser, one hit on the space bar will run the selected tests again. I like being able to use the keyboard without thinking!

I can't seem to get the CLI runner working however. I can run single tests, but not the whole suite. Each test gives errors like this:

Code: Select all

Exception29!
Invalid XML response: 
    in M2f_Module_Filter_UppercaseTest
Could not open input file: run.php
Exception30!
Invalid XML response: 
    in M2f_Module_Filter_BBCode_BBCodeTokenizerTest
Could not open input file: run.php
Exception31!
Invalid XML response: 
    in M2f_Web_ChainTest
Could not open input file: run.php
 
Any ideas? Also, a number of my individual tests fail (on CLI only), because of write permissions problems. What is your advice on running tests under particular users from the CLI? Do you run yours under your login name?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: E_STRICT error with ReflectionClass::newInstance()

Post by Chris Corbyn »

Are you on windows? I haven't tested the Sweety CLI runner for windows and knew it wouldn't work ;) It as on my list of things to do :)

EDIT | I also don't know where you got a copy of sweety from since it's not released, but it's possible you're using a very early version. I've made several changes to the CLI runner and the newest version is the one used by Swift's v4 branch:

https://swiftmailer.svn.sourceforge.net ... simplified (Subversion)

It's got some custom includes in the config.php which you'll probably want to remove.
georgeoc
Forum Contributor
Posts: 166
Joined: Wed Aug 09, 2006 4:21 pm
Location: London, UK

Re: E_STRICT error with ReflectionClass::newInstance()

Post by georgeoc »

Chris Corbyn wrote:Are you on windows?
Noooooooo. Take that back. I thought the Safari reference above was enough, forgetting that Windows has it as well now! I'm a Mac evangelist!
Chris Corbyn wrote:I haven't tested the Sweety CLI runner for windows and knew it wouldn't work ;) It as on my list of things to do :)

EDIT | I also don't know where you got a copy of sweety from since it's not released, but it's possible you're using a very early version. I've made several changes to the CLI runner and the newest version is the one used by Swift's v4 branch:

https://swiftmailer.svn.sourceforge.net ... simplified (Subversion)

It's got some custom includes in the config.php which you'll probably want to remove.
I believe that's where I got the source - copied from the latest Swift v4-simplified branch. I'll check.

As a start to the diagnosis, do you have any idea which script would produce the error "Could not open input file: run.php" ?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: E_STRICT error with ReflectionClass::newInstance()

Post by Chris Corbyn »

georgeoc wrote:
Chris Corbyn wrote:Are you on windows?
Noooooooo. Take that back. I thought the Safari reference above was enough, forgetting that Windows has it as well now! I'm a Mac evangelist!
Haha, sorry. Me too :)
As a start to the diagnosis, do you have any idea which script would produce the error "Could not open input file: run.php" ?
run.php has a few lines which try to auto-detect where the PHP executable is based upon the "_" (underscore) environment variable. It'll either be some wonky code there or you can override where it look for PHP by setting "SWEETY_PHP_EXE" in the config.php file.
Post Reply