开发者

Is PHP serialization a good choice for storing data of a small website modified by a single person

I'm planning a PHP website architecture. It will be a small website with few visitors and small set of data. The data is modified exclusively by a single user (administrator).

To make things easier, I don't want to bother with a real database or XML data. I think about storing all data through PHP serialization into several files. So for example 开发者_运维百科if there are several categories, I will store an array containing Category class instances for each category.

Are there any pitfalls using PHP serialization in those circumstances?


Use databases -- it is not that difficult and any extra time spent will be well learnt with database use.

The pitfalls I see are as Yehonatan mentioned:

1. Maintenance and adding functionality.
2. No easy way to query or look at data.
3. Very insecure -- take a look at "hackthissite.org". A lot of the beginning examples have to do with hacking where someone put the data hard coded in files.
4. Serialization will work for one array, meaning one table. If you have to do anything like have parent categories that have to match up to other data, not going to work so well.


The pitfalls come when with maintenance and adding functionality.

it is a very good way to learn but you will appreciate databases more after the lessons.


I tried to implement PHP serialization to store website data. For those who want to do the same thing, here's a feedback from the project started a few months ago and heavily modified since:

Pros:

  • It was very easy to load and save data. I don't have to write SQL queries, optimize them, etc. The code is shorter (with parametrized SQL queries, it may grow a lot).

  • The deployment does not require additional effort. We don't care about what is supported on the web server: if there is just PHP with no additional extensions, database servers, etc., the website will still work. Sqlite is a good thing, but it is not possible to install it on some servers, and it also requires a PHP extension.

  • We don't have to care about updating a database server, nor about the database server to use (thus avoiding the scenario where the customer wants to migrate from Microsoft SQL Server to Oracle, etc.).

  • We can add more properties to the objects without having to break everything (just like we can add other columns to the database).

Cons:

  • Like Kerry said in his answer, there is "no easy way to query or look at data". It means that any business intelligence/statistics cases are impossible or require a huge amount of work. By the way, some basic scenarios become extremely complicated. Let's say we store products and we want to know how much products there are. Instead of just writing select count(1) from Products, in my case it requires to create a PHP file just for that, load all data then count the number of items, sometimes by adding stuff manually.

  • Some changes required to implement data migration, which was painful and required more work than just executing an SQL query.

To conclude, I would recommend using PHP serialization for storing data of a small website modified by a single person only if all the following conditions are true:

  1. The deployment context is unknown and there are chances to have a server which supports only basic PHP with no extensions,

  2. Nobody cares about business intelligence or similar usages of the information,

  3. There will be no changes to the requirements with large impact on the data structure.


I would say use a small database like sqlite if you don't want to go through setting up a full db server. However I will also say that serializing an array and storing that in a text file is pretty dang fast. I've had to serialize an array with a few thousand records (a dump from a database) and used that as a temp database when our DB server was being rebuilt for a few days.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜