私は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(); ?>
に電話するまでフォームは表示されません。こうすることで、上記のように必要なコンストラクタを作成した後にフォームを再利用し、サイトの他の領域に共通の書式設定でフォームを表示することができます。
この場合、登録フォームは一度しか表示されませんが、これは再利用可能な書式設定であり、共通の要素に適用できます。
フォームをエコーした後にクラスを作成していることは明らかです。 – animuson
クラスのインスタンス、つまり何かを行うオブジェクトを作成する必要があります。 $ register = new register();を追加しようとします。 – Onheiron