0

私は OOP の学習を開始し、accountactions というクラスを作成しました。それがうまく書けたかどうか知りたいです。

クラスは次のファイルにあります: accountactions.class.php。

<?php

class accountactions(){

    public function register($login, $password, $email){

        //Zapisujemy dane wysłane formularzem
        $this->username = mysql_real_escape_string($login);
        $this->password = mysql_real_escape_string($password);
        $this->email = mysql_real_escape_string($email);

        //Hash password
        $this->password = md5(sha1($this->password));

        $db->simplequery("INSERT INTO radio_users(id, username, password, email) VALUES('', '$this->username', '$this->password', '$this->email')");

    }


}

?>

register.php ファイル:

<?php

    require_once("accountactions.class.php");

    $account = new accountactions();

    $account->register('samplelogin', 'samplepassword', 'sample@email');

?>

そして、私はこのフラグメントにいくつかの問題を抱えています:

$db->simplequery("INSERT INTO radio_users(id, username, password, email) VALUES('', '$this->username', '$this->password', '$this->email')");

db クラスをアカウント クラスに参加させるにはどうすればよいですか?

次のようなことができるモデルを維持したいと思います。

$account->register('$_POST['login']', '$_POST['password']', '$_POST['email']');

これを行うためのより良い方法がない限り。

OOPの初心者なので、ヒントやガイドラインをいただければ幸いです。

4

1 に答える 1

1

このコードはおおむね優れていますが、私が悪いと思う点がいくつかあります。まず、accountactions は不適切なクラス名であるため、命名規則に従う必要があると思います。OOP の場合、キャメルケースのバリエーションを使用する必要があると思います (accountActions または AccountActions のいずれか - 後者を使用することをお勧めします)。次に、クラス名の後に括弧があってはなりません。各中括弧を別々の行に入れることもお勧めしますが、それは個人的な好み次第です。それから、あなたの最初のコメントは洗練されたものです。誰もが理解できるように、すべてのコメント、変数名などは常に英語で書くことをお勧めします。次に、 register メソッドでクラスの属性に変数を割り当てていますが、以前にそれらを宣言していません (または、少なくともコードでそれを示していません)。また、挿入クエリであなたの 空の文字列 '' を id フィールドに挿入しようとしています (auto_increment を使用した一意の null 以外の符号なし整数であると想定しています。そうである場合は、クエリに含めないでください)。私はこのようにあなたのコードを書きます:

class AccountActions
{
    protected $Username;
    protected $Password;
    protected $Email;
    protected $DB;

    public function __construct()
    {
        $this->DB = //instantiate your database driver of choice here, e.g. mysqli
    }

    public function register($Username, $Password, $Email)
    {
        //We escape the provided values and populate the object's properties with them
        $this->Username = mysql_real_escape_string($Login);
        $this->Password = mysql_real_escape_string($Password);
        $this->Email = mysql_real_escape_string($Email);
        //Hash password
        $this->Password = md5(sha1($this->Password));
        $Query = "INSERT INTO radio_users(username, password, email) 
                  VALUES('$this->Username', '$this->Password', '$this->Email')";
        $this->DB->simplequery($Query);    
    }
}

db クラスをアカウント クラスに参加させるにはどうすればよいですか?

ここで何を意味しているのかわかりませんが、クラス内のデータベースドライバーにアクセスしたい場合は、データベースドライバーを格納するプロパティを追加し、コンストラクターでインスタンス化する必要があります (または保持する静的プロパティがある場合があります)。データベース ドライバ)。

また、タイトルの質問で何を意味するのかわからない-インナークラス(別のクラス内で宣言されたクラス)を使用したい場合-それらはPHPでは利用できません。

また、基本的な OOP を学んだ後は、PHP フレームワークを手に入れることをお勧めします。Zend Framework は私のお気に入りです。

于 2012-12-27T19:28:52.643 に答える