首页 > 编程技术 > php

php判断类是否存在函数 class_exists

发布时间:2016-11-25 16:50

如果我们要判断一个类是不是可以用,可以先使用class_exists函数来判断一下,下面来看几个例子。

//bool class_exists ( string $class_name [, bool $autoload = true ] )
//此功能是否给定的类被定义检查。this function checks whether or not the given class has been defined.

//返回true,如果class_name是一个定义的类,否则返回false。
//实例

 代码如下 复制代码
if (class_exists('myclass')) {
    $myclass = new myclass();
}

 

 代码如下 复制代码

function __autoload($class)
{
    include($class . '.php');

    // check to see whether the include declared the class
    if (!class_exists($class, false)) {
        trigger_error("unable to load class: $class", e_user_warning);
    }
}

if (class_exists('myclass')) {
    $myclass = new myclass();
}

本实例是要利用php array_diff函数来,删除数组中某个值元素哦,方法很简单用foreach再加array_diff函数就
 代码如下 复制代码
$a1 = array(array('blue','red','www.111cn.net'),array('black','pink','green'));
$a2 = array('aaa','pink','bbbb');
$str = 'red';
$a2[] = $str;
foreach($a1 as $key => $value)
{       
$a1[$key] = array_diff($value,$a2);
}
print_r($a1);


//更简单的做法

 代码如下 复制代码
foreach (array_diff($a1, $a2) as $_key_1) {
        $arr_new[$_key_1] = $arr_1[$_key_1];
}


/*
结果如下:

array
(
    [0] => array
        (
            [0] => blue
            [2] => yellow
        )


    [1] => array
        (
            [0] => black
            [2] => green
        )


)

array_diff语法

array array_diff ( array $array1 , array $array2 [, array $ ... ] )
对比较array1和array2返回差异。
*/
$array1 = array("a" => "green", "111cn.net", "blue", "red");
$array2 = array("b" => "green", "yellow", "red");
$result = array_diff($array1, $array2);

print_r($result);

/*
array
(
    [1] => blue
)

如果你要判断一些函数是不是可以用我们可以使用function_exists函数来检测哦,下面我们来看几个例子,希望对你会有帮助。

语法
bool function_exists ( string $function_name )
检查的定义的函数的列表,同时内置(内部)和用户定义的,为function_name。
返回值

返回true,如果function_name存在,是一个函数,否则返回false。
*/

if (function_exists('imap_open')) {
    echo "imap functions are available.www.111cn.net<br /> ";
} else {
    echo "imap functions are not available.<br /> ";
}


//function_exists returns false on null and empty string:

if (function_exists('')) {
                echo "empty string function exists ";
        }

        if (function_exists(null)) {
                echo "null function exists ";
        }
  
//如果您使用suhosin.executor.func.blacklist而不是在你的php.ini disabled_functions,function_exists将返回true为功能。我用这个有与suhosin.executor.func.blacklist和disabled_functions相同beahviour:

function suhosin_function_exists($func) {
    if (extension_loaded('suhosin')) {
        $suhosin = @ini_get("suhosin.executor.func.blacklist");
        if (empty($suhosin) == false) {
            $suhosin = explode(',', $suhosin);
            $suhosin = array_map('trim', $suhosin);
            $suhosin = array_map('strtolower', $suhosin);
            return (function_exists($func) == true && array_search($func, $suhosin) === false);
        }
    }
    return function_exists($func);
}

 代码如下 复制代码
$post =1;
$url = parse_url($url);
$host ='http://www.111cn.net';
$path ='/';
$query ='?action=111cn.net';
$port =80;
 
if($post) {
  $out = "post $path http/1.0 ";
  $out .= "accept: */* ";
  //$out .= "referer: $boardurl ";
  $out .= "accept-language: zh-cn ";
  $out .= "content-type: application/x-www-form-urlencoded ";
  $out .= "user-agent: $_server[http_user_agent] ";
  $out .= "host: $host ";
  $out .= 'content-length: '.strlen($post)." ";
  $out .= "connection: close ";
  $out .= "cache-control: no-cache ";
  $out .= "cookie: $cookie ";
  $out .= $post;
 } else {
  $out = "get $path http/1.0 ";
  $out .= "accept: */* ";
  //$out .= "referer: $boardurl ";
  $out .= "accept-language: zh-cn ";
  $out .= "user-agent: $_server[http_user_agent] ";
  $out .= "host: $host ";
  $out .= "connection: close ";
  $out .= "cookie: $cookie ";
 }
 $fp = @fsockopen(($ip ? $ip : $host), $port, $errno, $errstr, $timeout);
 if(!$fp)
 {
  return '';//note $errstr : $errno
 } else {
  return '成功访问';
 }
 /*
 fsockopen语法
 
 resource fsockopen ( string $hostname [, int $port = -1 [, int &$errno [, string &$errstr [, float $timeout = ini_get("default_socket_timeout") ]]]] )


 
 启动一个套接字连接到指定的主机的资源。
    php支持在互联网领域的目标和unix在所支持的套接字传输列表说明。所支持的传输列表也可以检索使用stream_get_transports()。
 
 该插座预设会被启用,阻塞模式。你可以切换到非阻塞模式使用stream_set_blocking()。
 
 如果上面实例看不懂,就来看个简的吧


*/

 

 代码如下 复制代码
$fp = fsockopen("www.111cn.net", 80, $errno, $errstr, 30);
 if (!$fp) {
  echo "$errstr ($errno)<br /> ";
 } else {
  $out = "get / http/1.1 ";
  $out .= "host: www.111cn.net ";
  $out .= "connection: close ";
  fwrite($fp, $out);
  while (!feof($fp)) {
   echo fgets($fp, 128);
  }
  fclose($fp);
 }

//需求:在所有连接后面添加一个request=xxx; 这个函数比preg_replace灵活性更强,要注意它所替换的内容为整个正则表达式的内容。
$content = '<a href="http://www.111cn.net/aaa.php">链接1</a><a href="http://www.111cn.net/aaa.php?id=111">链接2</a>';
function add_source($matches)
{
    if(strpos($matches[1], '?'))
    {
        return 'href="'.$matches[1].'&request=xxx"';  //注意,这里和下面都加上了正则里括号外的东西:href="
    }
    else
    {
        return 'href="'.$matches[1].'?request=xxx"';
    }
}
$content = preg_replace_callback('/href=['|"](.*?)['|"]/', 'add_source', $content);


//实例二


  // 此文本是用于 2002 年的,
  // 现在想使其能用于 2003 年
  $text = "april fools day is 04/01/2002 ";
  $text.= "last christmas was 12/24/2001 ";

  // 回调函数
  function next_year($matches) {
    // 通常:$matches[0] 是完整的匹配项
    // $matches[1] 是第一个括号中的子模式的匹配项
    // 以此类推
    return $matches[1].($matches[2]+1);
  }

  echo preg_replace_callback(
              "|(d{2}/d{2}/)(d{4})|",
              "next_year",
              $text);

  // 结果为:
  // april fools day is 04/01/2003
  // last christmas was 12/24/2002

标签:[!--infotagslink--]

您可能感兴趣的文章: