Undefined Property

Hi,

I’ve been test a class, but I’ve find an error, the follow is a part of a class EClass:




[1]      private $effectOptions = array();

[2]      private $validEffects = array('show', 'slideDown', 'fadeIn');


[316]    public function setEffectOptions($value)

[317]    {

[318]      if (!is_array($value))

[319]         throw new CException(Yii::t('EJqueryClass', 'effectOptions must be an array'));

[320]      $this->effectOptions = $value;

[321]    }



and this is the test:




[40]    public function testGetEffect() {

[41]	  EClass::setEffect( $this->effect );

[42]	  $this->assertEquals( $this->effect, EClass::getEffect() );	

[43]    } 

When I run the test, this error appear:

  1. EClassTest::testGetEffect

Undefined property: EClassTest::$validEffects

/var/www/testdrive/protected/tests/unit/EClass.php:318

/var/www/testdrive/protected/tests/unit/EClassTest.php:41

Someone can help me?

I think you should declare static methods and variables as such if you want to use them in that context. I know that you don’t have to do that in PHP but it makes it a lot easier to read and maybe this is the cause of this error. For example: I don’t quite get why you are using EClass::setEffect() and use a $this context in setEffectOptions() meaning that it is usable by an object ONLY, not by the class.

So you either make EClass static and use


public STATIC function setEffectOptions()



and define your variables as static too (public static $validEffects) or you could use objects to use methods and variables like this




$object=new EClass;

$object->setEffect($someOptions)



instead of




EClass::setEffect()



Maybe it helps. Besides that you may have to import EClass in your test class, if you haven’t done that already.

Hope it helps

Thanks Haensel, maybe I have to explain more mi code.

For example, I use this:




EClass::setEffect()



because the EClass has a constructor:




public function __construct($owner=null)

   {

      parent::__construct($owner);

      $this->setLanguage(Yii::app()->language);

   }



And if I use:




$object=new EClass;

$object->setEffect($someOptions)



When I run the test, shows me an error saying that I trying to get property of non-object.

I’ve been implemented your proposal about the STATIC methods but, I haven’t obtained positives results.

I keep looking the solution yet.