PHP的反射API(Reflection API)是面向对象编程(OOP)在PHP中的一个重要补充,它允许开发者在运行时检查类、接口、方法、属性等的详细信息,并可以动态地创建和操作对象。这种能力对于构建灵活、可扩展和可维护的应用程序非常有用。
下面是一些使用PHP反射API与面向对象编程结合的示例代码:
示例1:获取类的详细信息
class MyClass {
public $publicProperty;
protected $protectedProperty;
private $privateProperty;
public function myMethod() {
// ...
}
}
// 使用反射API获取类的详细信息
$reflectionClass = new ReflectionClass('MyClass');
// 获取类名
echo $reflectionClass->getName() . "\n";
// 获取类的所有属性(包括私有和保护属性)
$properties = $reflectionClass->getProperties();
foreach ($properties as $property) {
echo $property->getName() . "\n";
}
// 获取类的方法
$methods = $reflectionClass->getMethods();
foreach ($methods as $method) {
echo $method->getName() . "\n";
}
// 检查类是否实现了某个接口
if ($reflectionClass->implementsInterface('SomeInterface')) {
echo "MyClass implements SomeInterface\n";
}
示例2:动态创建对象并调用方法
class MyAnotherClass {
public function sayHello($name) {
echo "Hello, $name!";
}
}
// 使用反射API动态创建对象
$reflectionClass = new ReflectionClass('MyAnotherClass');
$instance = $reflectionClass->newInstance(); // 创建对象实例
// 使用反射API动态调用方法
$method = $reflectionClass->getMethod('sayHello');
$method->invoke($instance, 'World'); // 调用对象的方法并传入参数
示例3:处理私有属性和方法
虽然直接访问私有属性和方法是违反封装性的,但反射API允许我们这样做(通常用于调试或框架内部)。
class MyPrivateClass {
private $privateProperty = 'Secret';
private function privateMethod() {
return 'This is a private method.';
}
}
// 使用反射API访问私有属性和方法
$reflectionClass = new ReflectionClass('MyPrivateClass');
// 获取私有属性
$property = $reflectionClass->getProperty('privateProperty');
$property->setAccessible(true); // 设置为可访问
$instance = $reflectionClass->newInstance();
echo $property->getValue($instance) . "\n"; // 输出: Secret
// 获取私有方法
$method = $reflectionClass->getMethod('privateMethod');
$method->setAccessible(true); // 设置为可访问
echo $method->invoke($instance) . "\n"; // 输出: This is a private method.
- 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商品详情原数据