开发者

ideas for simple objects for day to day web-dev use?

Dang-I know this is a subjective question so will probably get booted off/locked, but I'll try anyway, because I don't know where else to ask (feel free to point me to a better place to ask this!)

I'm just wrapping my head around oop with PHP, but I'm still not using frameworks or anything.

I'd like to create several small simple objects that I could use in my own websites to better get a feel for them.

Can anyone recommend a list or a resource that could point me to say 10 day-to-day objects that people would use in basic websites?

The reason I'm asking is because I'm confusing myself a bit. For example, I was thinking of a "database connection" object, but then I'm just thinking that is just a function, and not really an "object"??

So the question is:

What are some examples of objects used in basic PHP websi开发者_如何学JAVAtes (not including "shopping cart" type websites)

Thanks!


Here's a few basic reusable objects you might have:

  • Session (identified by a cookie, stored server side)
  • User (username, password, etc.)
  • DBConnection (yes, this can be an object)
  • Comment (allow users to comment on things)

It sounds like you want to start to build your own web framework, which is a decent way to learn. Don't reinvent the wheel though. For a production site, you're probably better off using an existing framework.


Since you said you don't want to glue HTML and CSS again, you don't try this:

  • Create a WebForm class. This class is a container of form elements. It has methods to add and remove form elements. It has a getHTML() method that writes the form so that the user can input data. The same object is when a POST is made. It has a method to validate the input of the user; it delegates the validation to every form element and then does some kind of global validation. It has a process method that processes the form. It is final and checks whether validation has passed. If it passed it calls an abstract protected method that actually does the form-specific processing (e.g. insert rows into the DB). The form may be stored in the stored in session, or it may be re-built everytime (if it is stored in the session, it's easier to make multi-page forms).
  • Create a BaseFormElement and then several child classes like EmailElement, PhoneElement etc. These have also a getHTML() method that is called by WebForm::getHTML() and that prints the specific element. They have a validate() method that is called by WebForm::validate() and a getData() method that returns the properly validated and processed data of that element.

These are just some ideas. Some things may not make sense :p


I'd say database access would be the first most likely object - encapsulate your most common SQL requests into one class. If you make them abstract enough, you can use them for a wide variety of data access situations.

The way to think about class design/usage is to think of the class responsibility. You should be able to describe the class purpose in a short sentence (shorter than this...) i.e for database access object, you might say:

"provides API for common data access tasks"

If any of the methods in your data access class do something other than that, then you know they belong somewhere else.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜