0

抽象クラスについて質問があります

私は持っている

abstract class testMaster
{

    public function __construct($b, $a)
    {
        $this->a = $a;
        $this->b = $b;
    }

    public static function create($test)
    {
        //handle test
        switch($test) {
         case 'test1':
            require_once 'test1.class';
            return new test1($a, $v);
            break;
         case 'test2':
         case 'test3':
             require_once 'test2.class';
             return new test2($a, $v);
             break;


        }
    }

class tester extends testMaster{

    codes...

}

私の質問は

静的な ' ' メソッドを呼び出したい場合はcreate、どのように呼び出すか。

使用testMaster::create()しましたが、何も返されないようです。任意のヒント?本当にありがとう!

4

3 に答える 3

0

いくつかのこと(コメントを参照):

abstract class testMaster
{

  public function __construct($b, $a)
  {
      $this->a = $a;
      $this->b = $b;
  }

  public static function create($test)
  {

      switch($test) {
       case 'test1':
          require_once 'test1.class';
          return new test1($a, $v);
          break;
       case 'test2':
       case 'test3':
           require_once 'test2.class';
           return new test2($a, $v); // Missing a ')'
           break;


      }
  }

} // Missing '}'

class tester extends testMaster{ // Should be 'extends'


}
于 2013-03-29T19:47:04.660 に答える
0

を呼び出すだけではtestMaster::create()、$test に引数を渡していません。$test を空にする switch ケースはなく、defaultケースを宣言していないため、Create() の最後まで実行され、何も返されません。

于 2013-03-29T19:51:03.440 に答える
0

はい、構文エラーを修正したら、呼び出すことができます

$obj = testMaster::create('test1');

ところで、何らかのクラス ローダーメカニズムを使用し、require 呼び出しを使用しないことを強くお勧めします。

さらに、適切に設計されたテストに興味がある場合は、PHPUnitを参照してください。

于 2013-03-29T19:54:11.477 に答える