The method for increasing the memory limit of PHP is also my opinion on the use of static classes.
Source: Internet
Author: User
The method for increasing the memory limit of PHP is also my opinion on the use of static classes. of course, static classes have a good side. for example, they are suitable for implementing some stateless tool classes, but most of the time, my subjective tendency is very clear. I use more objects and use less static classes to avoid premature system solidification. By the way, I hope no one will tell me about the preaching of the static analogy object. The method for increasing the memory limit of PHP when running PHP is also my opinion on the use of static classes.
Of course, static classes have a good side. for example, they are suitable for implementing some stateless tool classes. However, most of the time, my subjective tendency is very clear. I use more objects and use less static classes, avoid premature system solidification. By the way, I hope no one will tell me about the preaching of the static analogy object.
When running the PHP program, the Error "Fatal Error: Allowed memory size of xxxxxx bytes exhausted" usually occurs. This means that the PHP script uses too much memory, the maximum memory allowed by the system is exceeded. To solve this problem, first check whether your program has allocated too much memory. if there is no problem with the program, you can use the following method to increase the memory limit (memory_limit) of PHP ).
Check the php memory limit value
To view this value, you need to create an empty php file, such as a view-php-info.php. Then paste the code into it.
Put the script on your Web server and call it in your browser. At this time, you can see the configuration information of your PHP environment, some of which are about "memory_limit", such:
Note: You can use this method to view other php parameter settings, not just memory_limit
What should memory_limit be set?
This is totally dependent on your application requirements. For example, Wordpress requires 32 MB to run the core code. Drupal 6 requires a minimum value of 16 MB and is recommended to be set to 32 MB. If you install many plug-ins, especially those that require image processing, you may need MB or higher memory.
How to set memory_limit
Method 1: php. ini
The simplest or most common method is to modify php. ini.
1. First, find the php. ini file that takes effect for your website. because php parameters can be set in multiple places, find the correct configuration file and make changes. If the above method creates a php File to view its Configuration parameters, you can find the "Loaded Configuration File" item. The following is an example:
For Linux users, you can find the corresponding Configuration File by executing "php-I | grep Loaded Configuration File. For Windows users, you can try to modify the php. ini under your php installation directory.
2. edit php. ini. in php. ini, find "memory_limit". if not, add this parameter at the end of the file. The following are some configuration examples:
Memory_limit = 128 M; you can change M to any value you want to set.
Save files
3. restart the web server. if the web server uses Apache, run the following command:
Httpd restart
In some cases, you may not be allowed to modify php. ini. For example, if you have purchased the VM service, but your service provider does not allow you to modify this file. You can use other methods to increase the value of memory_limit.
Method 2:. htaccess
Note: This method takes effect only when php is executed using the Apache module. Find the ". htaccess" file in the root directory of your website. if not, create one by yourself. Then put the following configuration into it:
Php_value memory_limit 128 M; you can change M to any value you want to set.
Method 3: modify the php memory settings at runtime
Add the following command line to your php code.
Ini_set ('memory _ limit ', '128m ');
Memory_limit modification failed
If you use a VM, you may fail to modify the memory_limit value. You need to contact your service provider to learn how to handle this problem. Generally, they limit the maximum value that can be set or they cannot be modified at all. If their environment does not meet your requirements, you may have to consider changing the host service provider.
There is a famous saying in iegg: existence is reasonable. The use of static classes is justified based on this argument. However, it is absolutely necessary. Once the code is too dependent on the static class, the solution to its deterioration is inevitable. This is like poppy, as a herb, has its pharmacological value, but if it is used in large quantities, it becomes.
What is a static class?
Static classes refer to classes that are called directly in static mode without being converted into objects by instance. The code is as follows:
Class Math {public static function ceil ($ value) {return ceil ($ value);} public static function floor ($ value) {return floor ($ value) ;}}?>
At this time, the role played by the class is more like a namespace, which may be the most direct reason many people prefer static classes.
Static class problems
Essentially, static classes are process-oriented, because they usually combine the originally process-oriented code sets mechanically. although the results exist in the form of classes, however, at this time, the class is more like a new emperor, so it can be said that the static class is actually dressed in an object-oriented shell, doing process-oriented things.
One of the object-oriented design principles: Interface-oriented programming, rather than implementation programming. What is the difference? For example: aside from the price, do you like computers with independent graphics cards or computers with integrated graphics cards? I think most people choose independent graphics cards. Independent graphics cards can be viewed as interface-oriented programming, while integrated graphics cards can be viewed as implementation programming. In this case, the disadvantages of programming are suddenly put on the paper: it loses the possibility of change.
The following is an example of a management system:
Class Article {public function save () {ArticleDAO: save () ;}}?>
Article implements the necessary domain logic and then delivers data persistence to ArticleDAO. ArticleDAO is a static class, which is as hard to change as the integrated video card that is soldered on the motherboard, suppose we may need to implement Mock ArticleDAO to test the code, but because the static class name is used for calling, it is equivalent to the specific implementation method already bound, Mock is almost impossible, of course, there are actually some ways to achieve this:
Class Article {private static $ dao = 'articledao '; public static funciton setDao ($ dao) {self ::$ dao = $ dao;} public static function save () {$ dao = self ::$ dao; $ dao: save () ;}}?>
With the intervention of variables, you can set which static class to use at runtime:
Article: setDao ('mockarticledao '); www. errenzhuan. cc; Article: save ();?>
Although this implementation method seems to solve the Mock problem, the original code it modified first violates the open and closed principles, and secondly introduces static variables, static variables are shared and may interfere with the execution of other code, so they are not a perfect solution.
In addition, the dynamic language feature can be used to implement Mock simply by using a different class definition file of require. However, this method also has drawbacks, and we assume that we are switching to www. errenzhuan. the implementation method needs to be changed multiple times in the cc script, but in fact we only have one chance of require. Otherwise, a duplicate definition error will occur.
Note: In some cases, static latency binding can also improve the testability of static classes. For more information, see PHPUnit.
Object value
How can I implement the document management system example if I abandon static classes and use objects instead? The code is as follows:
Class Article {private $ dao; public function _ construct ($ dao = null) {if ($ dao = null) {$ dao = new ArticleDAO ();} $ this-> setDao ($ dao);} public function setDao ($ dao) {$ this-> dao = $ dao;} public function save () {$ this-> dao-> save () ;}}?>
In fact, here we use the dependency injection technology that people often call to inject dependent objects through the constructor or Setter:
$ Article = new Article (new MockArticleDAO (); $ article-> save ();?>
The object has its own status, so the sharing status does not interfere with the execution of other code.
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