PEAR包的命名规则
每个类都根据包路径来命名,每个路径名以下划线来分割。
缺点: 类名冗长 ; 优点:类名不会重复、一看便知文件所在文件。
命名空间用于文件级别,声明是文件中的第一条语句。
前面的有学过。
as 和import ,在Yii 2.0中引入了命名空间的概念。
import XX文件
import XX路径 as XX
引用文件可以用相对路径也可以用绝对路径,也可以定义文件的路径,在APACHE或者php.ini的配置中
include_path = '' 注意linux用冒号分割,windows用分号分割、
在使用文件系统函数时(比如fopen()或require(),fopen()要指定第三个参数为true),如果没有找到相应的文件目录,就会在include_path下面寻找
也可以用函数 set_include_path() 仅为当前进程改变 include_path()来设置
get_include_path()来访问并加入自己的目录
set_include_path(get_include_path().":/home/john/phplib/")
PHP5引入了__autoload()拦截器方法来自动包含文件
function __autoload($class_name) {
$path = str_replace('_', '/', $class_name);
require_once $path . '.php';
}
// 这里会自动加载Http/File/Interface.php 文件
$a = new Http_File_Interface();
$map = array(
'Http_File_Interface' => 'C:/PHP/HTTP/FILE/Interface.php'
);
function __autoload($class_name) {
if (isset($map[$class_name])) {
require_once $map[$class_name];
}
}
// 这里会自动加载C:/PHP/HTTP/FILE/Interface.php 文件
$a = new Http_File_Interface();
class_exists() 和file_exists()
get_declared_classes()函数来获得脚本进程中定义的所有类的数组
instanceof操作符
检测函数是否存在的方法,有3种方式,in_array($method,get_class_methods($product))
is_callable(array($product,$method))
protected和private的类方法
会返回false
get_class_methods()来获得一个类中的所有方法
method_exists($product,$method)
只要方法存在就返回true
get_class_vars()了解类的属性
get_parent_class()来找到一个父类
is_subclass_of()是否是派生类
使用字符串来动态调用方法
call_user_func()方法来达到相同的目的。
调用函数 call_user_func("MyFunction")
调用类方法 call_user_func(array($myObj,"methodName"),20)
20是参数可选的。
call_user_func_array() 更好用,与call_user_func相同,只是把目标方法所需的任何参数当做数组来接受,与__call方法连用
与call()方法连用
http://blog.csdn.net/xiongmao000738/article/details/7283603
- class Person
- {
- function talk( $sound )
- {
- echo $sound;
- }
- function __call( $method , $args )
- {
- echo 'you call method ' . $method . '<br>';
- echo 'and the arguments are <br>';
- var_dump( $args );
- }
- }
- class PersonProxy
- {
- private $person;
- function __construct()
- {
- $this->person = new Person();
- }
- function __call( $method , $args )
- {
- call_user_func_array( array( $this->person , $method ) , $args );
- }
- }
- $person_proxy = new PersonProxy();
- $person_proxy->talk( 'thank you' );
域名解析是把域名指向网站空间IP
反射API
该扩展分析php程序,导出或提取出关于类、方法、属性、参数等的详细信息,包括注释。
Reflection可以说是对php库函数:“Classes/Objects 类/对象函数”的一个扩展。
主要用在通过程序检测现有php程序内部关于类、方法等信息,并做出处理。
2、API概览:
class Reflection { }
interface Reflector { }
class ReflectionException extends Exception { }
class ReflectionFunction implements Reflector { }
class ReflectionParameter implements Reflector { }
class ReflectionMethod extends ReflectionFunction { }
class ReflectionClass implements Reflector { }
class ReflectionObject extends ReflectionClass { }
class ReflectionProperty implements Reflector { }
class ReflectionExtension implements Reflector { }
http://www.jb51.net/article/51760.htm
多用反射API来做类测试工具。少用类和对象函数。ReflectionClass的构造方法接受类名作为它的唯一参数。