Home >Backend Development >PHP Tutorial >Sharing some interview questions and answers for Tencent PHP programmers

Sharing some interview questions and answers for Tencent PHP programmers

WBOY
WBOYOriginal
2016-07-25 08:59:251075browse
  1. class Sample implements iterator{
  2. private $var = array(1,2,3,4,5);
  3. public function __construct(){}
  4. public function rewind(){ reset($ this->var);}
  5. public function current(){return current($this->var);}
  6. public function key(){return key($this->var);}
  7. public function next (){return next($this->var);}
  8. public function valid(){return ($this->current()!==false);}
  9. }
  10. $s = new Sample();
  11. foreach($s as $k=>$v){ echo $k.'='.$v.'
    ';}
  12. ?>
Copy code

3. Briefly explain what is the garbage collection mechanism of PHP? There is a reference count for the variable, and the variable is destroyed when the count reaches 0.

4. To enable the object to be looped like an array, the properties must be private. (PHP5 implementation of Iterator mode, write a class to implement the Iterator interface) I remember that objects can also be foreach, or use reflection. If you really want to implement the iterative pattern, look at the design pattern or how to write it in Java.

5. Please write a piece of php code to ensure that multiple processes can write to the same file successfully at the same time. Code:

  1. function writeData($path, $mode, $data){

  2. $fp = fopen($path, $mode);
  3. $retries = 0;
  4. $max_retries = 100;
  5. do {
  6. if ($retries > 0) {
  7. usleep(rand(1, 10000));
  8. }
  9. $retries += 1;
  10. }while (!flock($fp, LOCK_EX) and $retries <= $max_retries);

  11. if ($retries == $max_retries) {

  12. return false;
  13. }
  14. fwrite($fp, "$datan");
  15. flock($fp, LOCK_UN );
  16. fclose($fp);
  17. return true;
  18. }

Copy code

6. What method do you use to check the execution efficiency of PHP scripts (usually script execution time) and database SQL efficiency (usually database Query time), and locate and analyze the bottlenecks of script execution and database query? Execution efficiency of PHP scripts 1. Timing in code script. 2. xdebug counts the number of function executions and the specific time for analysis. , it is best to use the tool winCacheGrind analysis 3. The online system uses strace to track specific system calls of related processes.

Database SQL efficiency SQL's explain(mysql) enables slow query log to record slow queries. It usually depends on whether the database design is reasonable, whether the requirements are reasonable, etc.

Want to join Tencent QQ? Master all the above interview questions, and then take your supplies and go to Tencent, haha! !



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn