| 添加到收藏夹 | 返回目录页 | 上一篇:加固PHP环境(转) |
References and Aliases are Different Mechanisms (zkarakaya )
References and Aliases are Different MechanismsAuthor: zkarakaya
Date 14/03/2001
<b>Aliasing and Referencing are completely different mechanisms in PHP.</b>
If you are Java or C++ programmer, you must be careful when using
Objects created on run-time.
<p>
Lets see an example;
<pre>
<?
class MyClass{
var $myData;
var $outManager;
cfunction MyClass($p){
$this->myData=$p;
$this->outManager = new MyOutManager($this);
}
cfunction display(){
$this->outManager->display();
}
}
class MyOutManager{
var $refObj;
cfunction MyOutManager(&$obj){
$this->refObj = &$obj;
}
cfunction display(){
echo $this->refObj->myData;
}
}
$myvar = new MyClass(10);
$myvar->myData = 20;
$myvar->display();
?>
</pre>
What value be the output of this program code. Many programmer will
say "20", but this is not correct. Output is 10. Why? Because we have
created an instance of MyClass type on the right hand side of assignment
operator, and gave an initial value of 10. In the constructor of MyClass,
we have send the memory location of that newly created instance to another
object of type MyOutManager, and hold this value in $refObj. Now the
reference count for this object is 1, which is $refObf property of
outManager instance. Lets continue the execution. Constructor has finished
its job and returned to assignment operator. PHP4 now creates a new
reference named $myvar to newly created object. Now the reference count
of that object is 2. Be careful that $myvar is not an alies. So that when
you execute the next statement, which assigns a value 20 to its property
named $myData, PHP4 creates a new instance of MyClass type, copies
the contents of old one which is also referenced by its member outManager.
And then changes the contents of myData to 20.
<p>
>From now on you will have two different instance of type MyClass.
Our intend was not this. So to correct this problem, use alias
on the object creation statement, that is use;
<pre>
$myvar = &new MyClass(10);
</pre>
This will solve the problem. So if you are C++ and Java programmer,
you must be careful in writing PHP codes.
<p>
This description does not conflict with the information given in the
<a href="http://www.zend.com/zend/art/ref-count.php"> PHP 4: Reference
Counting and Aliasing</a>
written by <a href="http://www.zend.com/comm_person.php?id=6" >Andi
Gutmans.</a>
<p>
Ziya Karakaya
↓相关文章:
- · 先做点好事,转点东东来,用PHP和MySQL构建一个数据库驱动的网站(-)
- · 用PHP和MySQL构建一个数据库驱动的网站(三)
- · 用PHP和MySQL构建一个数据库驱动的网站(四)
- · 用PHP和MySQL构建一个数据库驱动的网站(7)
- · 用PHP和MySQL构建一个数据库驱动的网站(十)
- · php中echo <<< 的应用
- · 正则表达式语法(转)
- · PHP的面向对象编程:开发大型PHP项目的方法(三)(转载)
- · PHP的面向对象编程:开发大型PHP项目的方法(五)(转载)
- · 规范3
- · 认知Web服务器
- · PHP脚本数据库功能详解(1)
- · PHP脚本数据库功能详解(3)
- · 第1次亲密接触PHP5(2)
- · 第1次亲密接触PHP5(1)
- · Oracle 常见问题解答
- · Oracle Faq(Oracle TAF的配置)
- · Oracle Faq(Oracle的版本)
- · PHP概述.
- · 数据库相关问题
- · 编译问题
- · 如何获得PHP相关资料
- · 提问的智慧
- · 用PHP实现banner轮换的程序代码
- · PHP取得客户端信息
- · PHP开发中接收复选框信息的方法
- · PHP技术:回帖脱衣服的图片实现
- · PHP程序加速探索之压缩输出gzip
- · 正确理解PHP程序编译时的错误信息
- · PHP和XSS跨站攻击
- · PHP调用java类常见配置错误
- · PHP中的MD5加密
- · PHP能得到你是从什么页面过来的,referer的用处
- · AJAX技术在PHP开发中的简单应用
- · PHP中路径问题的解决方案
- · PHP+MySQL分页显示示例分析
- · 教你如何用PHP制作静态网站的模板框架
- · PHP实现网页自动更新块
- · PHP与SQL注入攻击
- · 如何使用php开发高效的WEB系统
- · 用php生成EAN_13标准的条形码
- · PHP 的配置详细选项
- · SmartTemplate(适用于企业级PHP开发的模板引擎)
- · 用PHP写的QQ Client,可以登陆、登出、收发消息、添加好友
- · [PHP]关于时间计算的结总

