PHP反射API基础概念详解

368 篇文章 0 订阅

PHP反射API(Reflection API)是一个强大的工具,它允许我们在运行时检查类、接口、函数、方法和属性等结构,甚至可以动态地调用它们。下面我们将对PHP反射API的基础概念进行详解,并通过示例代码展示其用法。

1. 反射类(ReflectionClass)

ReflectionClass 类用于表示一个类,并提供了获取该类元数据的方法。

示例代码

<?php  
class MyClass {  
    public $publicProperty = 'Public';  
    protected $protectedProperty = 'Protected';  
    private $privateProperty = 'Private';  
  
    public function publicMethod() {  
        echo 'Public method called.';  
    }  
  
    protected function protectedMethod() {  
        echo 'Protected method called.';  
    }  
  
    private function privateMethod() {  
        echo 'Private method called.';  
    }  
}  
  
// 创建一个ReflectionClass对象  
$reflection = new ReflectionClass('MyClass');  
  
// 获取类名  
echo $reflection->getName() . "\n"; // 输出: MyClass  
  
// 获取类的所有属性(包括私有和受保护的)  
$properties = $reflection->getProperties();  
foreach ($properties as $property) {  
    echo $property->getName() . "\n"; // 输出: publicProperty, protectedProperty, privateProperty  
}  
  
// 获取类的公共方法  
$methods = $reflection->getMethods(ReflectionMethod::IS_PUBLIC);  
foreach ($methods as $method) {  
    echo $method->getName() . "\n"; // 输出: publicMethod  
}  
  
// 注意:由于私有和受保护的方法不能直接通过ReflectionClass获取,除非使用特定的标志,例如 ReflectionMethod::IS_PRIVATE  
  
// 使用反射来调用一个公共方法  
$instance = $reflection->newInstance(); // 创建一个MyClass的新实例  
$method = $reflection->getMethod('publicMethod');  
$method->invoke($instance); // 输出: Public method called.  
?>

2. 反射方法(ReflectionMethod)

ReflectionMethod 类用于表示类的方法,并提供了获取方法元数据的方法。

示例代码(使用ReflectionClass中的方法)

(上面ReflectionClass示例中已经包含了调用方法的部分)

3. 反射函数(ReflectionFunction)

ReflectionFunction 类用于表示函数,并提供了获取函数元数据的方法。

示例代码

<?php  
function myFunction() {  
    echo 'Function called.';  
}  
  
// 创建一个ReflectionFunction对象  
$reflectionFunction = new ReflectionFunction('myFunction');  
  
// 获取函数名  
echo $reflectionFunction->getName() . "\n"; // 输出: myFunction  
  
// 调用函数  
$reflectionFunction->invoke(); // 输出: Function called.  
?>

4. 反射参数(ReflectionParameter)

ReflectionParameter 类用于表示函数的参数,并提供了获取参数元数据的方法。通常通过ReflectionMethodReflectionFunctiongetParameters()方法获取。

示例代码(使用ReflectionMethod

<?php  
class MyClass {  
    public function myMethod($param1, $param2) {  
        // ...  
    }  
}  
  
// 创建一个ReflectionClass对象  
$reflection = new ReflectionClass('MyClass');  
  
// 获取myMethod的ReflectionMethod对象  
$method = $reflection->getMethod('myMethod');  
  
// 获取myMethod的参数列表  
$parameters = $method->getParameters();  
foreach ($parameters as $parameter) {  
    echo $parameter->getName() . "\n"; // 输出: param1, param2  
}  
?>
  • item_get 获得淘宝商品详情
  • item_get_pro 获得淘宝商品详情高级版
  • item_review 获得淘宝商品评论
  • item_fee 获得淘宝商品快递费用
  • item_password 获得淘口令真实url
  • item_list_updown 批量获得淘宝商品上下架时间
  • seller_info 获得淘宝店铺详情
  • item_search 按关键字搜索淘宝商品
  • item_search_tmall 按关键字搜索天猫商品
  • item_search_pro 高级关键字搜索淘宝商品
  • item_search_img 按图搜索淘宝商品(拍立淘)
  • item_search_shop 获得店铺的所有商品
  • item_search_seller 搜索店铺列表
  • item_search_guang 爱逛街
  • item_search_suggest 获得搜索词推荐
  • item_search_jupage 天天特价
  • item_search_coupon 优惠券查询
  • cat_get 获得淘宝分类详情
  • item_cat_get 获得淘宝商品类目
  • item_search_samestyle 搜索同款的商品
  • item_search_similar 搜索相似的商品
  • item_sku 获取sku详细信息
  • item_recommend 获取推荐商品列表
  • brand_cat 获取品牌分类列表
  • brand_cat_top 获取分类推荐品牌列表
  • brand_cat_list 得到指定分类的品牌列表
  • brand_keyword_list 得到指定关键词的品牌列表
  • brand_info 得到品牌相关信息
  • brand_product_list 得到指定品牌的产品
  • custom 自定义API操作
  • buyer_cart_add 添加到购物车
  • buyer_cart_remove 删除购物车商品
  • buyer_cart_clear 清空购物车
  • buyer_cart_list 获取购物车的商品列表
  • buyer_cart_order 将购物车商品保存为订单
  • buyer_order_list 获取购买到的商品订单列表
  • buyer_order_detail 获取购买到的商品订单详情
  • buyer_order_express 获取购买到的商品订单物流
  • buyer_order_message 获取购买到的订单买家留言
  • buyer_address_list 收货地址列表
  • buyer_address_clear 清除收货地址
  • buyer_address_remove 删除收货地址
  • buyer_address_modify 修改收货地址
  • buyer_address_add 添加收货地址
  • buyer_info 买家信息
  • buyer_token 买家token
  • seller_order_list 获取卖出的商品订单列表
  • seller_order_detail 获取卖出的商品订单详情
  • seller_order_close 卖家关闭一笔交易
  • seller_order_message 获取或修改卖出去的订单备注
  • seller_auction_list 商品可上下架商品列表
  • seller_auction 商品上下架
  • seller_item_add 商品上传
  • upload_img 上传图片到淘宝
  • img2text 图片识别商品接口
  • tbk_order_query 淘宝客订单查询
  • item_list_weight 批量获取商品信息
  • item_history_price 获取商品历史价格信息
  • item_get_app 获得淘宝app商品详情原数据
  • 7
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值