Php:uchome Analysis

Source: Internet
Author: User
Tags bbcode zend framework
1.1.1 Framework core of code structure 1.1.2uchome

Uchome is an SNS system, but also has the deep PHP technology accumulation of Hong Sing company's commercial products, there are many places worth learning, you can use it to set up a vertical SNS site, you can also learn some of his skills, improve their own code level, improve code quality.

For PHP development, lightweight things are always favored, although the framework of the Cake,zend framework is good, but for smaller systems, it's too much, and using things cut from the uchome, you can make the system good enough in a simple, modest way.

Uchome the whole structure is very simple, although it is also the MVC model, but strictly speaking, and did not implement a set of models, directly with the database returned objects, view is a set of self-implemented template method to achieve the separation of functional code and interface design.

Uchome's framework compares the core of several files:

Source/function_

Provides commonly used functions such as getting post strings, getting user identities, inserting data, updating data, logging, template calling interfaces, where getstr,insertable,updatetable several functions are very practical and convenient.

Look at the function parameters of gestr;

function Getstr ($string, $length, $in _slashes=0, $out _slashes=0, $censor =0, $bbcode =0, $html =0)

Intercept strings, escape characters, HTML, BBCode all inside

The way to insert and update data is also very simple, instead of splicing SQL yourself, to insert data as an example:

function inserttable ($tablename, $insertsqlarr, $returnid =0, $replace = False)

We insert employee information, you can write this:

Inserttable (' Staffs ', Array (' name ' = = ' Hankshuang ', ' post ' = = ' de ', ' age ' =>25), 1)

Source/function_

Template function file, provide the parsing method of the template file, is called by the templates in the Function_common file, the template directory file is parsed into a file, the code of the templates syntax is escaped , using loop to refer to foreach, parsing code is not complex, interested can read the next Function_ file in the Parse_template method, is called preg_replace to do regular expression substitution, in this way, you do not have to write mixed

Source/function_

The most important way to provide cached writes and updates is to cache_write the method and see the declaration:

function Cache_write ($name, $var, $values)

If I want to cache employee data, then for $staff = Array (' name ' = ' Hankshuang ', ' age ' =>25)

The method is called

Cache_write (' Staffcache ', ' staff ', $staff)

Next time you want to use this content, directly include the data directory of this Data_staff file, you get $staff this object, the principle is simple, it is very convenient to use.

With these three files, it is enough for you to build a fast and practical system, of course, there are some very useful functions in uchome, such as processing, mail delivery, timing tasks These, the design is very clever, for improving PHP development skills is very helpful.

1.1.3uchome Configuration Table Cache

As SNS site, compared to the CMS site, dynamic requirements, generate static page value is not too big, change factors too much, so uchome cache on the one hand is the view cache, that is, the template cache, on the other hand, each page needs to cache the configuration file, Reduce the load on the database server.

In home/, the read of the configuration is this:

Configuration file

if (! @include_once (s_root. /data/data_ ')) {

Include_once (S_root. /source/function_ ');

Config_cache ();//Update configuration file

}

The Data/data_ file is generated by Config_cache () to see the specific implementation in the Function_ file:

Update configuration file

function Config_cache ($updatedata =true) {

Global $_sglobal, $_sconfig;

$_sconfig = Array ();

$query = $_sglobal[' db ']->query (' SELECT * from '. Tname (' config '));

while ($value = $_sglobal[' db ']->fetch_array ($query)) {

if ($value [' var '] = = ' privacy ') {

$value [' datavalue '] = Empty ($value [' DataValue '])? Array (): Unserialize ($value [' datavalue ']);

}

$_sconfig[$value [' var '] = $value [' DataValue '];

}

Cache_write (' config ', ' _sconfig ', $_sconfig);

..... Omit part

}

Read the data in the configuration table into the $_sconfig array, and write to the cache using Cache_write, Cache_write code as follows:

function Cache_write ($name, $var, $values) {

$cachefile = S_root. /data/data_ '. $name. PHP ';

$cachetext = "

"If (!defined (' In_uchome ')) exit (' Access Denied '); \ r \ n".

' $ '. $var. ' = '. Arrayeval ($values).

"\r\n?>";

if (!swritefile ($cachefile, $cachetext)) {

Exit ("File: $cachefile write error.");

}

}

Did you see that? This code is to save the hash variable into a file that can be directly included in the code.

For example you write a cache_write ("test", "Hanks", Array ("Strong" =>1, "good" =>1, "nice" =>1))

Finally you will get a Data_ file with the contents of

if (!defined (' In_uchome ')) exit (' Access Denied ');

$hanks = Array ("Strong" =>1, "good" =>1, "nice" =>1);

More complex nesting can also be arrayeval to convert strings.

Generating PHP files for caching is the most common mode of uchome, and template is the same, simple and practical

1.1.4uchome Database access

In the Space_ file, we see this query code:

$query = $_sglobal[' db ']->query ("select * from". Tname (' friend '). " WHERE uid= ' $space [uid] ' and status= ' 1 ' LIMIT 0,6 ");

What kind of an object is $_sglobal[' db '? Find the answer in the Function_ file:

function Dbconnect () {

Global $_sglobal, $_SC;

Include_once (S_root. /source/class_ ');

$_sglobal[' db '] = new Dbstuff;

$_sglobal[' db ']->charset = $_sc[' Dbcharset '];

$_sglobal[' db ']->connect ($_sc[' dbhost ', $_sc[' Dbuser '], $_sc[' DBPW '), $_sc[' dbname '], $_sc[' pconnect ']);

}

Originally a Dbstuff object in the Class_ file

The most important method in the Dbstuff class is the Connect () and the query () method, connect is responsible for opening the database connection, the specific data to rely on query, query core code is as follows:

$func = $type = = ' unbuffered ' && @function_exists (' Mysql_unbuffered_query ')?

' Mysql_unbuffered_query ': ' mysql_query ';

if (! ( $query = $func ($sql, $this->link)) && $type! = ' SILENT ') {

$this->halt (' MySQL Query Error ', $sql);

}

Just looked at the time did not find the query statement how to execute, and later found to be a small trick, declared a variable $func, after the type judgment, assigned to "Mysql_query", and then $func (), is equal to mysql_query (), This skill is familiar to Ror comrades certainly not unfamiliar.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.