简体   繁体   中英

OOP Classes, how to put class in class

I've started to learn an OOP and I've built a class called accountactions, and I would like to know if I did a good job writing it.

The class is in the file: 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')");

    }


}

?>

The register.php file:

<?php

    require_once("accountactions.class.php");

    $account = new accountactions();

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

?>

And I am having some issues with this fragment:

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

How do I join my db class to my account class?

I'd like to keep a model where I can do something like:

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

Unless there is a better way to do this.

Im newbie in OOP so any tips and guidelines are appreciated.

This code is mainly good, but there are some things I consider bad. First off, I think you should follow some naming convention, because accountactions is a bad clas name. For OOP I think you should use some variation of camelcase (so either accountActions or AccountActions - I suggest you use the latter). Then, there shouldn't be the parentheses after the class name. I also suggest you to put each curly bracket in separate line, but it's up to your personal preference. Then, your first comment is in polish - I suggest you to always write all comments, variable names etc. in English, just because everyone will understand it. Then in the register method you're assigning variables to class' attributes, but you haven't declared them before (or at least you haven't shown it to us in the code). Also in the insert query you're trying to insert emtpy string '' to the id field (I assume it's unique, non-null unsigned integer with auto_increment - if it is, you shouldn't include it in your query). I would write your code this way:

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);    
    }
}

How do I join my db class to my account class?

Not sure what you meant here, but if you want to have access to some database driver inside your class, you should add a property that will store the database driver and instantiate it in the constructor (or you may have a static property that will hold the database driver).

Also not sure what you meant in your title question - if you want to use inner classes (classes declared inside another classes) - they're not available in PHP.

I also encourage you to pick up some PHP framework after you learn the basic OOP - Zend Framework is my favourite.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM