php_lang_ref:Language Reference >> References Explained >> Returning References

PHP引用返回详解
本文通过一个具体的PHP案例,深入解析了引用返回的概念及其工作原理。文章展示了如何通过引用返回实现变量间的绑定,使得一个变量的变化可以即时反映到另一个变量上。
<?php
// +----------------------------------------------------------------------
// | Created by im-server.
// +----------------------------------------------------------------------
// | Language Reference >> References Explained >> Returning References
// +----------------------------------------------------------------------
// | Author: alexander <gt199899@gmail.com>
// +----------------------------------------------------------------------
// | Datetime: 2017-07-16 18:25
// +----------------------------------------------------------------------
// | Perfect Is Shit
// +----------------------------------------------------------------------

/**
 * 引用返回将函数返回对应的变量和函数被赋值的变量指向同一个内容;
 * 官网的定义:引用返回用在当想用函数找到引用应该被绑定在哪一个变量上面时;
 * case1:官网示例
 */

namespace case1;

class foo
{
    public $value = 42;

    public function &getValue()
    {
        return $this->value;
    }
}

$obj = new foo;
$myValue = &$obj->getValue(); // $myValue is a reference to $obj->value, which is 42.
$obj->value = 2;
echo $myValue;                // prints the new value of $obj->value, i.e. 2.
/**
 * 输出:
 * 2
 */
Table users { id int [pk, increment] username varchar [not null, unique] password varchar [not null] email varchar phone varchar address text create_time timestamp [default: `CURRENT_TIMESTAMP`] status int [default: 1, comment: '0:禁用 1:正常'] role varchar [default: 'USER', comment: 'USER:普通用户 ADMIN:管理员'] points int [default: 0] total_spent decimal [default: 0.00] } Table categories { id int [pk, increment] name varchar [not null] description text create_time timestamp [default: `CURRENT_TIMESTAMP`] status int [default: 1] } Table products { id int [pk, increment] name varchar [not null] description text price decimal [not null] stock int [default: 0] image varchar category_id int create_time timestamp [default: `CURRENT_TIMESTAMP`] status int [default: 1, comment: '0:下架 1:上架'] sales_count int [default: 0, comment: '销量'] rating decimal [default: 0.00, comment: '平均评分'] update_time timestamp Ref: categories.id > products.category_id } Table cart_items { id int [pk, increment] user_id int [not null] product_id int [not null] quantity int [default: 1] create_time timestamp [default: `CURRENT_TIMESTAMP`] update_time timestamp [default: `CURRENT_TIMESTAMP`, on_update: `CURRENT_TIMESTAMP`] Ref: users.id > cart_items.user_id Ref: products.id > cart_items.product_id Unique: (user_id, product_id) } Table orders { id int [pk, increment] user_id int [not null] order_no varchar [not null, unique] total_amount decimal [not null] status int [default: 0, comment: '0:待付款 1:已付款 2:已发货 3:已完成 4:已取消'] address text phone varchar receiver_name varchar create_time timestamp [default: `CURRENT_TIMESTAMP`] pay_time timestamp ship_time timestamp complete_time timestamp payment_method varchar [comment: '支付方式:ALIPAY/WEIXIN/BANK'] payment_status int [default: 0, comment: '0:未支付 1:已支付 2:支付失败'] Ref: users.id > orders.user_id } Table order_items { id int [pk, increment] order_id int [not null] product_id int [not null] product_name varchar [not null] price decimal [not null] quantity int [not null] subtotal decimal [not null] Ref: orders.id > order_items.order_id Ref: products.id > order_items.product_id } Table product_reviews { id int [pk, increment] user_id int [not null] product_id int [not null] order_id int [not null] rating int [not null, check: `rating >= 1 AND rating <= 5`] content text images text [comment: '评价图片,多个图片用逗号分隔'] create_time timestamp [default: `CURRENT_TIMESTAMP`] status int [default: 1, comment: '0:隐藏 1:显示'] Ref: users.id > product_reviews.user_id Ref: products.id > product_reviews.product_id Ref: orders.id > product_reviews.order_id } Table payment_records { id int [pk, increment] order_id int [not null] payment_no varchar [not null, unique] amount decimal [not null] payment_method varchar [not null] status int [default: 0, comment: '0:待支付 1:支付成功 2:支付失败 3:已退款'] create_time timestamp [default: `CURRENT_TIMESTAMP`] pay_time timestamp transaction_id varchar [comment: '第三方支付交易号'] Ref: orders.id > payment_records.order_id } Table user_permissions { id int [pk, increment] user_id int [not null] permission varchar [not null, comment: '权限名称'] create_time timestamp [default: `CURRENT_TIMESTAMP`] Ref: users.id > user_permissions.user_id Unique: (user_id, permission) } 帮我修改
07-08
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值