开发者

PHP OOP Concept into Practice

开发者 https://www.devze.com 2023-02-24 17:42 出处:网络
What I have at the moment is a review form that is written in a procedural manor. I have wanted to change a few things and realise in the process the entire review section of the site would be better

What I have at the moment is a review form that is written in a procedural manor. I have wanted to change a few things and realise in the process the entire review section of the site would be better in OOP from past experience.

The data flow is as follows:

HTML Form => Submit => jQuery Validation => ? => PHP Validation => Add Record

What I have the issue with is understanding wh开发者_运维百科at the correct way to pass POST data to a class is being that the confirmation message appears using AJAX/jQuery, or even a form that is has a standard thank you page for that matter.

I have the class ready to accept the POST data passing it into the methods to validate and finally add to the database but not sure what the correct protocol is to get it there in the first place.

The ways I have done it in the past is pass the form onto a process page which in turn forwards on again. I'm unsure on what the correct way is when attempting to complete the above.

Thanks :)


I will tell you only how I do it, and perhaps you'll find something useful in my code, again there is no a exact way to do things in programming, but you can hear opinions and form your own.

Suppose I have a jQuery form to create new users passed onto the server (Ajax or not, doesn't matter) and you performed some validation using the client side, on the server I do something like this:

$user = BaseDTO::ParseFromRequest("User");

if ($user->IsValid()) {
  //... send some error back to javascript
}

UserRepository::Attach($user);

I have a BaseDTO(Data Transfer Object), which knows how to look for the properties of a given class (thus the "User" parameter) and checks if the provided values are correct, then I use a Repository object to save the database.

I sincerely hope I can help

0

精彩评论

暂无评论...
验证码 换一张
取 消