dqm4977 2016-02-12 10:13
浏览 29

将PHP OOP与真实世界用户的表单注册相关联

Though I am new to the PHP world, and will like help with this:

After users visits your website and fills out the form, the user proceeds to submission. Once the user clicks on submit (with PHP as the server framework) they expect to be redirected to their page.

Now how do I relate the OOP new object part to the new user for the network? See what I mean in brief:

My OOP side:

class  Users {
protected $firstname;
protected $lastname;
protected $password;
protected $email;


public function __construct($firstname, $lastname, $password, $email){
$this->firstname = $firstname;
$this->lastname = $lastname;
$this->password = $firstname;
$this->email = $email;
}

//Some function here...

}

$newuser = new Users();
$anotheruser = new Users();

So now after form validation side when the new user must have clicked submit, and he/she expects to go to their page, how do I relate the form validation side with my OOP for the real world process?

In the end, the new user now sees, welcome Mark (as a result of this: $this->firstname) on the other page.

Thank you...

  • 写回答

1条回答 默认 最新

  • duanmei1922 2016-02-13 12:22
    关注

    You need to understand that there are much more things to do here than just define the User class.

    The general flow for the web app is something like this:

    1. User enters his data on the registration form
    2. Form is POSTed to your php code
    3. You application class routes the POST request to the specific code which handles user registration
    4. User data is validated, if something is wrong, the error response is sent back to the browser
    5. User data is saved to the database
    6. User session is started, so your application knows that he/she is logged in
    7. Successful response is sent to the browser (or your code redirect browser to the profile page)

    Now, on the code level, there will be many objects. I assume that your application will have a single entry point, some base index.php file which accepts all the requests and then routes to the handlers.

    So you will have (this is not a real code, just a structure):

    index.php

    $app = new WebApplication();
    $app->run();
    

    framework/Application.php

    class WebApplication {
    
       public function run() {
           $request = new Request();  // will parse data from $_GET, $_POST, etc
           $router = new UrlRouter($request);
           $controller = $router->resolveRequest(); // according to the rules you define, it will find
                                                    // what controller to create
                                                    // controllers handle specific requests
           $controller->run();
       }
    
    }
    

    framework/Controller.php

    class Controller {   // You will have a base class for controllers
       // ...
    }
    

    application/controllers/UserController.php

    class UserController extends Controller {   
    
       public function actionRegister() {
           // Create user validator and pass the Request object into it
           $validator = UserValidator($this->getRequest());
           if (!$validator->validate()) {
               // render the HTML with error
               $this->render('error', array('message'=>$validator->getError()));
               // OR send as json
               $this->sendJson(array(
                   'status'=>'error', 'message'=>$validator->getError()
               ));
           } else {
               $user = new User(
                   $this->getRequest()->get('email'),
                   $this->getRequest()->get('password')
                   // ...
               );
               $dbTable = new UsersTable();
               $dbTable->save($user);
               $this->startSession($user);
       }
    
    }
    

    application/models/User.php

    class User extends Model {
    
        protected $email;
        protected $firstname;
        //... 
    
        public function __construct($email, $password) {
            // ...
        }
    
    }
    

    And so on, we need to define UserValidator, UserTable and everything else.

    So the User class in your question is only a small part of the big picture. If you want to do the OOP properly, you are going to have a lot of classes and objects. They may be similar to what I describe above or they may differ a lot, but the overall idea will be the same - the features you need should be split over a relatively small objects and you combine them to build your application.

    If you want to do this, the best thing is to learn how it is done in popular frameworks. You don't need to dive deep from the start and learn the code, just go through the few tutorials to see how it looks for the framework user.

    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值