PHPソースからのテスト(phpt)を使用したPHPインタビューの準備

PHPを手動でアセンブルする場合(この場合、バージョン7.0.7を検討しています)、make installの前にmake testコマンドを実行する必要があります。これにより、 testsフォルダー内のすべてのテストが実行され、コマンドラインから結果を送信できます。 このフォルダーを見ると、classs、func、basicなどの名前のフォルダーがすぐに目立つようになります。



実際、インタビューでは、ある種の構文(一般的な用語で++ $ i / $ i ++などの退屈なタスク(クロスワード))と一般的なもの(OOPなど)の両方について質問することがよくあります。 人気のある瞬間は、インターフェイス、抽象クラスの継承です。その本質は、一般に、正しいOOPに違反すること、または機能(能力)に関する候補者の知識の深さを明らかにすることです。 これらはテストとテストを色分けするポイントです。したがって、私の意見では、テストを流に見ることは非常に役立ちます(期待される結果はテストに記述されています)。 より説得力を持たせるには、同じコードを実行してみてください。 グーグルで調べて、これがPHPで実装されている(動作している)理由を見つけます。



以下は、例、インタビューでの基本的な質問、およびソースコードからのテストです。



: tests/classes/abstract_by_interface_001.phpt
      
      







 --TEST-- ZE2 An abstract method may not be called --FILE-- <?php class Root { } interface MyInterface { function MyInterfaceFunc(); } abstract class Derived extends Root implements MyInterface { } class Leaf extends Derived { function MyInterfaceFunc() {} } var_dump(new Leaf); class Fails extends Root implements MyInterface { } ?> ===DONE=== --EXPECTF-- object(Leaf)#%d (0) { } Fatal error: Class Fails contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (MyInterface::MyInterfaceFunc) in %sabstract_by_interface_001.php on line %d
      
      





説明からわかるように、FailsクラスはMyInterfaceインターフェイスを継承しましたが、MyInterfaceFunc()インターフェイスから関数本体を決定しなかったため、致命的なエラーになります。 インターフェイスのドキュメントを参照してください。



次のテストに進みましょう。



 : tests/classes/abstract_by_interface_002.phpt
      
      







この場合、同じインターフェイスMyInterfaceには、静的として宣言されたメソッドのシグネチャ定義があります。



 interface MyInterface { static function MyInterfaceFunc(); } .... class Fails extends Root implements MyInterface { } ?> ===DONE=== --EXPECTF-- object(Leaf)#%d (0) { } Fatal error: Class Fails contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (MyInterface::MyInterfaceFunc) in %sabstract_by_interface_002.php on line %d
      
      





予想されるエラーからわかるように、この場合、このインターフェイスを継承するクラスでメソッドを定義する必要もあります。



または、通常のクラスで抽象メソッドを定義できるかどうかについての非常に一般的な質問です。



 : tests/classes/abstract_derived.phpt
      
      







 --TEST-- ZE2 A derived class with an abstract method must be abstract         --SKIPIF-- <?php if (version_compare(zend_version(), '2.0.0-dev', '<')) die('skip ZendEngine 2 needed'); ?> --FILE-- <?php class base { } class derived extends base { abstract function show(); } ?> ===DONE=== <?php exit(0); ?> --EXPECTF-- Fatal error: Class derived contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (derived::show) in %sabstract_derived.php on line %d
      
      







または、ここに1つの素晴らしいテストがあります。



 : tests/classes/abstract_redeclare.phpt
      
      







 <?php class pass { function show() { echo "Call to function show()\n"; } } class fail extends pass { abstract function show(); } echo "Done\n"; // Shouldn't be displayed ?>
      
      





show()メソッドはabstractでオーバーライドできません。



func testディレクトリに移動し、例として、静的および後置インクリメント/デクリメントを使用したテストに行きましょう。



 : tests/func/002.phpt
      
      







 --TEST-- Static variables in functions --FILE-- <?php function blah() { static $hey=0,$yo=0; echo "hey=".$hey++.", ",$yo--."\n"; } blah(); blah(); blah(); if (isset($hey) || isset($yo)) { echo "Local variables became global :(\n"; } --EXPECT-- hey=0, 0 hey=1, -1 hey=2, -2
      
      







最後に、あなたをあまり退屈させないために、興味深いテストをバグ28800で行います。



 --TEST-- Bug #28800 (Incorrect string to number conversion for strings starting with 'inf') --FILE-- <?php $strings = array('into', 'info', 'inf', 'infinity', 'infin', 'inflammable'); foreach ($strings as $v) { echo ($v+0)."\n"; } ?> --EXPECT--
      
      





それだけです、時間がかかってくれてありがとう。



All Articles