首頁 後端開發 php教程 PHP源码来操作memcached服务

PHP源码来操作memcached服务

Jun 23, 2016 pm 01:44 PM

如何使用PHP源码来操作memcached服务

如果管理员不让我们去加载 memcache.dll 文件,我们可以直接通过源码操作

看高手的源代码也很有意思!

特点:
1.不需要开启memcache扩展
2.使用fsockopen()套接字连接memcached
3.同样执行执行CRUD

引用别人写的一个类:

memcached-client.php

<?php //// +---------------------------------------------------------------------------+// | memcached client, PHP                                                     |// +---------------------------------------------------------------------------+// | Copyright (c) 2003 Ryan T. Dean <rtdean@cytherianage.net>                 |// | All rights reserved.                                                      |// |                                                                           |// | Redistribution and use in source and binary forms, with or without        |// | modification, are permitted provided that the following conditions        |// | are met:                                                                  |// |                                                                           |// | 1. Redistributions of source code must retain the above copyright         |// |    notice, this list of conditions and the following disclaimer.          |// | 2. Redistributions in binary form must reproduce the above copyright      |// |    notice, this list of conditions and the following disclaimer in the    |// |    documentation and/or other materials provided with the distribution.   |// |                                                                           |// | THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR      |// | IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |// | OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.   |// | IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,          |// | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT  |// | NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |// | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY     |// | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT       |// | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF  |// | THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.         |// +---------------------------------------------------------------------------+// | Author: Ryan T. Dean <rtdean>                            |// | Heavily influenced by the Perl memcached client by Brad Fitzpatrick.      |// |   Permission granted by Brad Fitzpatrick for relicense of ported Perl     |// |   client logic under 2-clause BSD license.                                |// +---------------------------------------------------------------------------+//// $TCAnet$///** * This is the PHP client for memcached - a distributed memory cache daemon. * More information is available at http://www.danga.com/memcached/ * * Usage example: * * require_once 'memcached.php'; *  * $mc = new memcached(array( *              'servers' => array('127.0.0.1:10000',  *                                 array('192.0.0.1:10010', 2), *                                 '127.0.0.1:10020'), *              'debug'   => false, *              'compress_threshold' => 10240, *              'persistant' => true)); * * $mc->add('key', array('some', 'array')); * $mc->replace('key', 'some random string'); * $val = $mc->get('key'); * * @author  Ryan T. Dean <rtdean> * @package memcached-client * @version 0.1.2 */// {{{ requirements// }}}// {{{ constants// {{{ flags/** * Flag: indicates data is serialized */define("MEMCACHE_SERIALIZED", 1 * @package memcached-client */class memcached{   // {{{ properties   // {{{ public   /**    * Command statistics    *    * @var     array    * @access  public    */   var $stats;      // }}}   // {{{ private   /**    * Cached Sockets that are connected    *    * @var     array    * @access  private    */   var $_cache_sock;      /**    * Current debug status; 0 - none to 9 - profiling    *    * @var     boolean    * @access  private    */   var $_debug;      /**    * Dead hosts, assoc array, 'host'=>'unixtime when ok to check again'    *    * @var     array    * @access  private    */   var $_host_dead;      /**    * Is compression available?    *    * @var     boolean    * @access  private    */   var $_have_zlib;      /**    * Do we want to use compression?    *    * @var     boolean    * @access  private    */   var $_compress_enable;      /**    * At how many bytes should we compress?    *    * @var     interger    * @access  private    */   var $_compress_threshold;      /**    * Are we using persistant links?    *    * @var     boolean    * @access  private    */   var $_persistant;      /**    * If only using one server; contains ip:port to connect to    *    * @var     string    * @access  private    */   var $_single_sock;      /**    * Array containing ip:port or array(ip:port, weight)    *    * @var     array    * @access  private    */   var $_servers;      /**    * Our bit buckets    *    * @var     array    * @access  private    */   var $_buckets;      /**    * Total # of bit buckets we have    *    * @var     interger    * @access  private    */   var $_bucketcount;      /**    * # of total servers we have    *    * @var     interger    * @access  private    */   var $_active;   // }}}   // }}}   // {{{ methods   // {{{ public functions   // {{{ memcached()   /**    * Memcache initializer    *    * @param   array    $args    Associative array of settings    *    * @return  mixed    * @access  public    */   function memcached ($args)   {      $this->set_servers($args['servers']);      $this->_debug = $args['debug'];      $this->stats = array();      $this->_compress_threshold = $args['compress_threshold'];      $this->_persistant = isset($args['persistant']) ? $args['persistant'] : false;      $this->_compress_enable = true;      $this->_have_zlib = function_exists("gzcompress");            $this->_cache_sock = array();      $this->_host_dead = array();   }   // }}}   // {{{ add()   /**    * Adds a key/value to the memcache server if one isn't already set with     * that key    *    * @param   string   $key     Key to set with data    * @param   mixed    $val     Value to store    * @param   interger $exp     (optional) Time to expire data at    *    * @return  boolean    * @access  public    */   function add ($key, $val, $exp = 0)   {      return $this->_set('add', $key, $val, $exp);   }   // }}}   // {{{ decr()   /**    * Decriment a value stored on the memcache server    *    * @param   string   $key     Key to decriment    * @param   interger $amt     (optional) Amount to decriment    *    * @return  mixed    FALSE on failure, value on success    * @access  public    */   function decr ($key, $amt=1)   {      return $this->_incrdecr('decr', $key, $amt);   }   // }}}   // {{{ delete()   /**    * Deletes a key from the server, optionally after $time    *    * @param   string   $key     Key to delete    * @param   interger $time    (optional) How long to wait before deleting    *    * @return  boolean  TRUE on success, FALSE on failure    * @access  public    */   function delete ($key, $time = 0)   {      if (!$this->_active)         return false;               $sock = $this->get_sock($key);      if (!is_resource($sock))         return false;            $key = is_array($key) ? $key[1] : $key;            $this->stats['delete']++;      $cmd = "delete $key $time\r\n";      if(!fwrite($sock, $cmd, strlen($cmd)))      {         $this->_dead_sock($sock);         return false;      }      $res = trim(fgets($sock));            if ($this->_debug)         printf("MemCache: delete %s (%s)\n", $key, $res);            if ($res == "DELETED")         return true;      return false;   }   // }}}   // {{{ disconnect_all()   /**    * Disconnects all connected sockets    *    * @access  public    */   function disconnect_all ()   {      foreach ($this->_cache_sock as $sock)         fclose($sock);      $this->_cache_sock = array();   }   // }}}   // {{{ enable_compress()   /**    * Enable / Disable compression    *    * @param   boolean  $enable  TRUE to enable, FALSE to disable    *    * @access  public    */   function enable_compress ($enable)   {      $this->_compress_enable = $enable;   }   // }}}   // {{{ forget_dead_hosts()   /**    * Forget about all of the dead hosts    *    * @access  public    */   function forget_dead_hosts ()   {      $this->_host_dead = array();   }   // }}}   // {{{ get()   /**    * Retrieves the value associated with the key from the memcache server    *    * @param  string   $key     Key to retrieve    *    * @return  mixed    * @access  public    */   function get ($key)   {      if (!$this->_active)         return false;               $sock = $this->get_sock($key);            if (!is_resource($sock))         return false;               $this->stats['get']++;            $cmd = "get $key\r\n";      if (!fwrite($sock, $cmd, strlen($cmd)))      {         $this->_dead_sock($sock);         return false;      }            $val = array();      $this->_load_items($sock, $val);            if ($this->_debug)         foreach ($val as $k => $v)            printf("MemCache: sock %s got %s => %s\r\n", $sock, $k, $v);            return $val[$key];   }   // }}}   // {{{ get_multi()   /**    * Get multiple keys from the server(s)    *    * @param   array    $keys    Keys to retrieve    *    * @return  array    * @access  public	*  get('key1')   keys=array('key1','key2');    */   function get_multi ($keys)   {      if (!$this->_active)         return false;               $this->stats['get_multi']++;            foreach ($keys as $key)      {         $sock = $this->get_sock($key);         if (!is_resource($sock)) continue;         $key = is_array($key) ? $key[1] : $key;         if (!isset($sock_keys[$sock]))         {            $sock_keys[$sock] = array();            $socks[] = $sock;         }         $sock_keys[$sock][] = $key;      }            // Send out the requests      foreach ($socks as $sock)      {         $cmd = "get";         foreach ($sock_keys[$sock] as $key)         {            $cmd .= " ". $key;         }         $cmd .= "\r\n";                  if (fwrite($sock, $cmd, strlen($cmd)))         {            $gather[] = $sock;         } else         {            $this->_dead_sock($sock);         }      }            // Parse responses      $val = array();      foreach ($gather as $sock)      {         $this->_load_items($sock, $val);      }            if ($this->_debug)         foreach ($val as $k => $v)            printf("MemCache: got %s => %s\r\n", $k, $v);                  return $val;   }   // }}}   // {{{ incr()   /**    * Increments $key (optionally) by $amt    *    * @param   string   $key     Key to increment    * @param   interger $amt     (optional) amount to increment    *    * @return  interger New key value?    * @access  public    */   function incr ($key, $amt=1)   {      return $this->_incrdecr('incr', $key, $amt);   }   // }}}   // {{{ replace()   /**    * Overwrites an existing value for key; only works if key is already set    *    * @param   string   $key     Key to set value as    * @param   mixed    $value   Value to store    * @param   interger $exp     (optional) Experiation time    *    * @return  boolean    * @access  public    */   function replace ($key, $value, $exp=0)   {      return $this->_set('replace', $key, $value, $exp);   }   // }}}   // {{{ run_command()   /**    * Passes through $cmd to the memcache server connected by $sock; returns     * output as an array (null array if no output)    *    * NOTE: due to a possible bug in how PHP reads while using fgets(), each    *       line may not be terminated by a \r\n.  More specifically, my testing    *       has shown that, on FreeBSD at least, each line is terminated only    *       with a \n.  This is with the PHP flag auto_detect_line_endings set    *       to falase (the default).    *    * @param   resource $sock    Socket to send command on    * @param   string   $cmd     Command to run    *    * @return  array    Output array    * @access  public    */   function run_command ($sock, $cmd)   {      if (!is_resource($sock))         return array();            if (!fwrite($sock, $cmd, strlen($cmd)))         return array();               while (true)      {         $res = fgets($sock);         $ret[] = $res;         if (preg_match('/^END/', $res))            break;         if (strlen($res) == 0)            break;      }      return $ret;   }   // }}}   // {{{ set()   /**    * Unconditionally sets a key to a given value in the memcache.  Returns true    * if set successfully.    *    * @param   string   $key     Key to set value as    * @param   mixed    $value   Value to set    * @param   interger $exp     (optional) Experiation time    *    * @return  boolean  TRUE on success    * @access  public    */   function set ($key, $value, $exp=0)   {      return $this->_set('set', $key, $value, $exp);   }   // }}}   // {{{ set_compress_threshold()   /**    * Sets the compression threshold    *    * @param   interger $thresh  Threshold to compress if larger than    *    * @access  public    */   function set_compress_threshold ($thresh)   {      $this->_compress_threshold = $thresh;   }   // }}}   // {{{ set_debug()   /**    * Sets the debug flag    *    * @param   boolean  $dbg     TRUE for debugging, FALSE otherwise    *    * @access  public    *    * @see     memcahced::memcached    */   function set_debug ($dbg)   {      $this->_debug = $dbg;   }   // }}}   // {{{ set_servers()   /**    * Sets the server list to distribute key gets and puts between    *    * @param   array    $list    Array of servers to connect to    *    * @access  public    *    * @see     memcached::memcached()    */   function set_servers ($list)   {      $this->_servers = $list;      $this->_active = count($list);      $this->_buckets = null;      $this->_bucketcount = 0;            $this->_single_sock = null;      if ($this->_active == 1)         $this->_single_sock = $this->_servers[0];   }   // }}}   // }}}   // {{{ private methods   // {{{ _close_sock()   /**    * Close the specified socket    *    * @param   string   $sock    Socket to close    *    * @access  private    */   function _close_sock ($sock)   {      $host = array_search($sock, $this->_cache_sock);      fclose($this->_cache_sock[$host]);      unset($this->_cache_sock[$host]);   }   // }}}   // {{{ _connect_sock()   /**    * Connects $sock to $host, timing out after $timeout    *    * @param   interger $sock    Socket to connect    * @param   string   $host    Host:IP to connect to    * @param   float    $timeout (optional) Timeout value, defaults to 0.25s    *    * @return  boolean    * @access  private    */   function _connect_sock (&$sock, $host, $timeout = 0.25)   {      list ($ip, $port) = explode(":", $host);      if ($this->_persistant == 1)      {         $sock = @pfsockopen($ip, $port, $errno, $errstr, $timeout);      } else      {         $sock = @fsockopen($ip, $port, $errno, $errstr, $timeout);      }            if (!$sock)         return false;      return true;   }   // }}}   // {{{ _dead_sock()   /**    * Marks a host as dead until 30-40 seconds in the future    *    * @param   string   $sock    Socket to mark as dead    *    * @access  private    */   function _dead_sock ($sock)   {      $host = array_search($sock, $this->_cache_sock);      list ($ip, $port) = explode(":", $host);      $this->_host_dead[$ip] = time() + 30 + intval(rand(0, 10));      $this->_host_dead[$host] = $this->_host_dead[$ip];      unset($this->_cache_sock[$host]);   }   // }}}   // {{{ get_sock()   /**    * get_sock    *    * @param   string   $key     Key to retrieve value for;    *    * @return  mixed    resource on success, false on failure    * @access  private    */   function get_sock ($key)   {      if (!$this->_active)         return false;      if ($this->_single_sock !== null)         return $this->sock_to_host($this->_single_sock);            $hv = is_array($key) ? intval($key[0]) : $this->_hashfunc($key);            if ($this->_buckets === null)      {         foreach ($this->_servers as $v)         {            if (is_array($v))            {               for ($i=0; $i_buckets = $bu;         $this->_bucketcount = count($bu);      }            $realkey = is_array($key) ? $key[1] : $key;      for ($tries = 0; $tries_buckets[$hv % $this->_bucketcount];         $sock = $this->sock_to_host($host);         if (is_resource($sock))            return $sock;         $hv += $this->_hashfunc($tries . $realkey);      }            return false;   }   // }}}   // {{{ _hashfunc()   /**    * Creates a hash interger based on the $key    *    * @param   string   $key     Key to hash    *    * @return  interger Hash value    * @access  private    */   function _hashfunc ($key)   {      $hash = 0;      for ($i=0; $i<strlen ord return _incrdecr perform increment on string command to key it interger amount adjust new value of private function if>_active)         return null;               $sock = $this->get_sock($key);      if (!is_resource($sock))         return null;               $key = is_array($key) ? $key[1] : $key;      $this->stats[$cmd]++;      if (!fwrite($sock, "$cmd $key $amt\r\n"))         return $this->_dead_sock($sock);               stream_set_timeout($sock, 1, 0);      $line = fgets($sock);      if (!preg_match('/^(\d+)/', $line, $match))         return null;      return $match[1];   }   // }}}   // {{{ _load_items()   /**    * Load items into $ret from $sock    *    * @param   resource $sock    Socket to read from    * @param   array    $ret     Returned values    *    * @access  private    */   function _load_items ($sock, &$ret)   {      while (1)      {         $decl = fgets($sock);         if ($decl == "END\r\n")         {            return true;         } elseif (preg_match('/^VALUE (\S+) (\d+) (\d+)\r\n$/', $decl, $match))         {            list($rkey, $flags, $len) = array($match[1], $match[2], $match[3]);            $bneed = $len+2;            $offset = 0;                        while ($bneed > 0)            {               $data = fread($sock, $bneed);               $n = strlen($data);               if ($n == 0)                  break;               $offset += $n;               $bneed -= $n;               $ret[$rkey] .= $data;            }                        if ($offset != $len+2)            {               // Something is borked!               if ($this->_debug)                  printf("Something is borked!  key %s expecting %d got %d length\n", $rkey, $len+2, $offset);               unset($ret[$rkey]);               $this->_close_sock($sock);               return false;            }                        $ret[$rkey] = rtrim($ret[$rkey]);            if ($this->_have_zlib && $flags & MEMCACHE_COMPRESSED)               $ret[$rkey] = gzuncompress($ret[$rkey]);            if ($flags & MEMCACHE_SERIALIZED)               $ret[$rkey] = unserialize($ret[$rkey]);         } else          {            if ($this->_debug)               print("Error parsing memcached response\n");            return 0;         }      }   }   // }}}   // {{{ _set()   /**    * Performs the requested storage operation to the memcache server    *    * @param   string   $cmd     Command to perform    * @param   string   $key     Key to act on    * @param   mixed    $val     What we need to store    * @param   interger $exp     When it should expire    *    * @return  boolean    * @access  private    */   function _set ($cmd, $key, $val, $exp)   {      if (!$this->_active)         return false;         //get_sock就是去获取到memcached服务器连接      $sock = $this->get_sock($key);      if (!is_resource($sock))         return false;               $this->stats[$cmd]++;            $flags = 0;            if (!is_scalar($val))      {         $val = serialize($val);         $flags |= MEMCACHE_SERIALIZED;         if ($this->_debug)            printf("client: serializing data as it is not scalar\n");      }            $len = strlen($val);            if ($this->_have_zlib && $this->_compress_enable &&           $this->_compress_threshold && $len >= $this->_compress_threshold)      {         $c_val = gzcompress($val, 9);         $c_len = strlen($c_val);                  if ($c_len _debug)               printf("client: compressing data; was %d bytes is now %d bytes\n", $len, $c_len);            $val = $c_val;            $len = $c_len;            $flags |= MEMCACHE_COMPRESSED;         }      }      if (!fwrite($sock, "$cmd $key $flags $exp $len\r\n$val\r\n"))         return $this->_dead_sock($sock);               $line = trim(fgets($sock));            if ($this->_debug)      {         if ($flags & MEMCACHE_COMPRESSED)            $val = 'compressed data';         printf("MemCache: %s %s => %s (%s)\n", $cmd, $key, $val, $line);      }      if ($line == "STORED")         return true;      return false;   }   // }}}   // {{{ sock_to_host()   /**    * Returns the socket for the host    *    * @param   string   $host    Host:IP to get socket for    *    * @return  mixed    IO Stream or false    * @access  private    */   function sock_to_host ($host)   {     	  if (isset($this->_cache_sock[$host]))         return $this->_cache_sock[$host];            $now = time();      list ($ip, $port) = explode (":", $host);      if (isset($this->_host_dead[$host]) && $this->_host_dead[$host] > $now ||          isset($this->_host_dead[$ip]) && $this->_host_dead[$ip] > $now)         return null;               if (!$this->_connect_sock($sock, $host))         return $this->_dead_sock($host);               // Do not buffer writes      stream_set_write_buffer($sock, 0);            $this->_cache_sock[$host] = $sock;            return $this->_cache_sock[$host];   }   // }}}   // }}}   // }}}}// }}}?> </strlen></rtdean></rtdean>
登入後複製

代用代码:

<?php require_once 'memcached-client.php';    $mc = new memcached(array(               'servers' => array('127.0.0.1:11211'), //连接的memcacheip和端口               'debug'   => false, //是否debug               'compress_threshold' => 10240, /*最大压缩*/               'persistant' => true)); /*是否是持久连接*/   $mc->set('key1', array('some', 'array')); // $mc->replace('key', 'some random string');   $val = $mc->get('key1');	var_dump($val);	//修改	$mc->replace('key1', "北京");	$val = $mc->get('key1');	var_dump($val);	//删除	$mc->delete('key1');  $val = $mc->get('key1');	echo "删除后";	var_dump($val);
登入後複製



本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

熱門話題

Java教學
1664
14
CakePHP 教程
1421
52
Laravel 教程
1315
25
PHP教程
1266
29
C# 教程
1239
24
PHP和Python:比較兩種流行的編程語言 PHP和Python:比較兩種流行的編程語言 Apr 14, 2025 am 12:13 AM

PHP和Python各有優勢,選擇依據項目需求。 1.PHP適合web開發,尤其快速開發和維護網站。 2.Python適用於數據科學、機器學習和人工智能,語法簡潔,適合初學者。

說明PHP中的安全密碼散列(例如,password_hash,password_verify)。為什麼不使用MD5或SHA1? 說明PHP中的安全密碼散列(例如,password_hash,password_verify)。為什麼不使用MD5或SHA1? Apr 17, 2025 am 12:06 AM

在PHP中,應使用password_hash和password_verify函數實現安全的密碼哈希處理,不應使用MD5或SHA1。1)password_hash生成包含鹽值的哈希,增強安全性。 2)password_verify驗證密碼,通過比較哈希值確保安全。 3)MD5和SHA1易受攻擊且缺乏鹽值,不適合現代密碼安全。

PHP行動:現實世界中的示例和應用程序 PHP行動:現實世界中的示例和應用程序 Apr 14, 2025 am 12:19 AM

PHP在電子商務、內容管理系統和API開發中廣泛應用。 1)電子商務:用於購物車功能和支付處理。 2)內容管理系統:用於動態內容生成和用戶管理。 3)API開發:用於RESTfulAPI開發和API安全性。通過性能優化和最佳實踐,PHP應用的效率和可維護性得以提升。

什麼是HTTP請求方法(獲取,發布,放置,刪除等),何時應該使用? 什麼是HTTP請求方法(獲取,發布,放置,刪除等),何時應該使用? Apr 09, 2025 am 12:09 AM

HTTP請求方法包括GET、POST、PUT和DELETE,分別用於獲取、提交、更新和刪除資源。 1.GET方法用於獲取資源,適用於讀取操作。 2.POST方法用於提交數據,常用於創建新資源。 3.PUT方法用於更新資源,適用於完整更新。 4.DELETE方法用於刪除資源,適用於刪除操作。

PHP:網絡開發的關鍵語言 PHP:網絡開發的關鍵語言 Apr 13, 2025 am 12:08 AM

PHP是一種廣泛應用於服務器端的腳本語言,特別適合web開發。 1.PHP可以嵌入HTML,處理HTTP請求和響應,支持多種數據庫。 2.PHP用於生成動態網頁內容,處理表單數據,訪問數據庫等,具有強大的社區支持和開源資源。 3.PHP是解釋型語言,執行過程包括詞法分析、語法分析、編譯和執行。 4.PHP可以與MySQL結合用於用戶註冊系統等高級應用。 5.調試PHP時,可使用error_reporting()和var_dump()等函數。 6.優化PHP代碼可通過緩存機制、優化數據庫查詢和使用內置函數。 7

解釋self ::,parent ::和static :: in php oop中的區別。 解釋self ::,parent ::和static :: in php oop中的區別。 Apr 09, 2025 am 12:04 AM

在PHPOOP中,self::引用當前類,parent::引用父類,static::用於晚靜態綁定。 1.self::用於靜態方法和常量調用,但不支持晚靜態綁定。 2.parent::用於子類調用父類方法,無法訪問私有方法。 3.static::支持晚靜態綁定,適用於繼承和多態,但可能影響代碼可讀性。

PHP如何安全地上載文件? PHP如何安全地上載文件? Apr 10, 2025 am 09:37 AM

PHP通過$\_FILES變量處理文件上傳,確保安全性的方法包括:1.檢查上傳錯誤,2.驗證文件類型和大小,3.防止文件覆蓋,4.移動文件到永久存儲位置。

PHP類型提示如何起作用,包括標量類型,返回類型,聯合類型和無效類型? PHP類型提示如何起作用,包括標量類型,返回類型,聯合類型和無效類型? Apr 17, 2025 am 12:25 AM

PHP類型提示提升代碼質量和可讀性。 1)標量類型提示:自PHP7.0起,允許在函數參數中指定基本數據類型,如int、float等。 2)返回類型提示:確保函數返回值類型的一致性。 3)聯合類型提示:自PHP8.0起,允許在函數參數或返回值中指定多個類型。 4)可空類型提示:允許包含null值,處理可能返回空值的函數。

See all articles