私は bfavaretto に同意します。彼のコード スニペットは確実に機能します。私は通常、クラス、関数、および出力を別々のファイルに保存して、次のようにプロジェクト間の使いやすさを少し高めたいと考えています。もう少し PHP コーディングが必要かもしれませんが、最終的には、再利用性が大いに役立ちました。私はあなたのプロジェクトを次のように整理します。
ロード中の元のページ:
<?php require './includes/class/register.class.php'; ?>
<?php include './includes/registerModules.php'; ?>
$register = new register(); //Calls the constructor for class 'register'
<?php registerForm(); ?> // Runs the registerForm() function. Nothing is output to the browser until this function is called. That way you can include a test to see if a user is already logged in and use a header redirect (which requires that no output code be sent to the browser before the header function is called) if you so desire.
./includes/class/register.class.php
class register {
function __construct() {
echo 'SYSTEM: This is the register page';
}
}
./includes/registerModules.php
function registerForm(){
echo '<form action="http://abc.org/test" method="POST">
<input type="username" value="">
<input type="password" value="">
<input type="submit" value="Register">
</form>';
}
このようにして、register.class.php に追加できるクラスが必要な場合はいつでも、登録フォームを表示する必要がないページにクラスを含めることができます。必要な後処理など。クラスはすべてそこにあり、適切なコンストラクター (この場合は$register = new register();
) を使用してそれらをインスタンス化できます。
registerModules.php に関数を含めたので、目的の関数を呼び出す場所であれば、関数の出力は関数呼び出しが行われた場所に表示されます。書式設定を容易にします。たとえば、 を呼び出すまでフォームは表示されません<?php registerForm(); ?>
。そうすれば、上記のように必要なコンストラクターを作成した後にフォームを再利用し、サイトの他の領域で共通の書式設定でフォームを表示できます。
この場合、登録フォームは 1 回しか表示されない可能性が高いことはわかっていますが、これは再利用可能な書式設定であり、任意の共通要素に適用できます。