php函数
round() 函数对浮点数进行四舍五入。 floor() 函数向下舍入为最接近的整数。 mt_rand() 使用 Mersenne Twister 算法返回随机整数。例如想要 5 到 15(包括 5 和 15)之间的随机数,用 mt_rand(5, 15)。
get_object_vars:
get_object_vars() 从字面我们可以猜到,这个函数是针对类的一个方法;
语法:
array get_object_vars(object $obj )
它的作用就是返回类中所有的非静态方法:
<?php
class object1 {
private $a = NULL;
public $b = 123;
public $c = 'public';
private $d = 'private';
static $e = 'static';
public function test(){
echo "<pre>";
print_r(get_object_vars($this));
echo "<pre>";
}
}
$test = new object1();
//print_r(get_object_vars($test));
$test->test();
?>
输出:
Array (
[a] =>
[b] => 123
[c] => public
[d] => private
)
如果把//print_r(get_object_vars($test));的注释打开的话,则输出:
Array (
[b] => 123
[c] => public
)
也就是说在外面只会弹出public的非静态的属性;
array_slice(array,start,length,preserve)
array_slice(array,start,length,preserve)
array_slice() 函数在数组中根据条件取出一段值,并返回。
$a=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow","e"=>"brown");
print_r(array_slice($a,1,2));
array_shift() 将 array 的第一个单元移出并作为结果返回,将 array 的长度减一并将所有其它单元向前移动一位。所有的数字键名将改为从零开始计数,文字键名将不变。
spl_autoload_register()和__autoload()
//__autoload用法
function __autoload($classname)
{
$filename = "./class/".$classname.".class.php";
if (is_file($filename))
{
include $filename;
}
}
//spl_autoload_register用法
spl_autoload_register('load_class');
function load_class($classname)
{
$filename = "./class/".$classname.".class.php";
if (is_file($filename))
{
include $filename;
}
}
class ClassAutoloader {
public function __construct() {
spl_autoload_register(array($this, 'loader'));
}
private function loader($className) {
echo 'Trying to load ', $className, ' via ', __METHOD__, "()\n";
include $className . '.php';
}
}
$autoloader = new ClassAutoloader();
$obj = new Class1();
$obj = new Class2();
__autoload()函数只能存在一次,spl_autoload_register()能注册多个函数
function a () {
include 'a.php';
}
function b () {
include 'b.php';
}
spl_autoload_register('a');
spl_autoload_register('b');
get_magic_quotes_gpc()
magic_quotes_gpc函数在php中的作用是判断解析用户提示的数据,如包括有:post、get、cookie过来的数据增加转义字符“ ”,以确保这些数据不会引起程序,特别是数据库语句因为特殊字符引起的污染而出现致命的错误
当magic_quotes_gpc=On的时候,函数get_magic_quotes_gpc()就会返回1 当magic_quotes_gpc=Off的时候,函数get_magic_quotes_gpc()就会返回0
PHP的反射类ReflectionClass、ReflectionMethod
$magicMethods = array(
'__construct', '__destruct', '__call', '__callStatic',
'__get', '__set', '__isset', '__unset',
'__sleep', '__wakeup', '__toString', '__invoke',
'__set_state', '__clone'
);
$bootstrapRef = new ReflectionClass("Bootstrap");
$methodList = $bootstrapRef->getMethods();
foreach ($methodList as $methodTemp)
{
$f = substr($methodTemp->name, 0, 1);
if ('_' === $f && !in_array($methodTemp->name, $magicMethods))
{
$method = new ReflectionMethod($bootstrap, $methodTemp->name);
if (true === $method->isPublic())
{
if ($method->getParameters())
{
$bootstrap->{$methodTemp->name}($this->_dispatcher);
}
else
{
$bootstrap->{$methodTemp->name}();
}
}
}
}
$ref_method = new ReflectionMethod($obj, $method);
$this->checkException(!$ref_method->isPublic(), 'UnPHPExceptionLoadFailedAction', 'This action("' . $method . '") not open permissions!');
extension_loaded — 检查一个扩展是否已经加载
bool extension_loaded ( string $name )
bool dl ( string $library )
dl — 运行时载入一个 PHP 扩展
载入指定参数 library 的 PHP 扩展。
使用 extension_loaded() 来测试指定的扩展是否已经激活。 这既能用于内建的扩展也可以用于动态加载的扩展(既可以通过 php.ini 也可以通过 dl())。
// 加载一个扩展的例子,基于操作系统
if (!extension_loaded('sqlite')) {
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
dl('php_sqlite.dll');
} else {
dl('sqlite.so');
}
}
// 或者,PHP_SHLIB_SUFFIX 常量在 PHP 4.3.0 后有效
if (!extension_loaded('sqlite')) {
$prefix = (PHP_SHLIB_SUFFIX === 'dll') ? 'php_' : '';
dl($prefix . 'sqlite.' . PHP_SHLIB_SUFFIX);
}

浙公网安备 33010602011771号