当我在PHP中实例化一个类时,是否获得了指向该对象的指针? [英] When I instantiate a class in PHP, do I get a pointer to the object?

查看:151
本文介绍了当我在PHP中实例化一个类时,是否获得了指向该对象的指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

或者,我的变量保存对象本身吗?

Or, does my variable hold the object itself?

例如,当我说:

$obj = new ClassOne();

$ obj是指向内存中创建的对象的指针吗?它仅保存对象的内存地址吗?还是它保留了对象本身?

is the $obj a pointer to the object created in the memory? Does it hold only the memory address to the object? Or does it hold the object itself?

例如,当我说

$obj = new SomeOtherClass();

将像JAVA中那样对ClassOne对象进行垃圾收集,还是像C ++中那样导致内存泄漏?

Will ClassOne object be garbage collected like in JAVA, or will it cause a memory-leak like in C++?

推荐答案

简而言之,C ++和Java中的对象模型有些不同:

Briefly, the object models in C++ and Java are a bit different:

  • C ++具有不受限制的变量:每个对象类型都可以作为变量的对象类型出现.换句话说,变量可以是任何类型的对象. (但是并非所有变量都是对象(例如引用)!)此外,所有变量都具有作用域,因此,所有对象(即变量)的生存期也将自动确定范围.只有动态分配的对象永远不能是变量,并且只能通过指针和引用进行处理.

  • C++ has unconstrained variables: Every object type can occur as the type of an object that is a vari­able. In other words, variables can be objects of any type. (But not all va­ri­ables are objects (e.g. re­fe­rences)!) Moreover, all variables are scoped, and thus the lifetime of all objects-that-are-variables is also scoped automatically. Only dynamically-allocated objects can never be variables, and they can only be handled via pointers and references.

在Java中,如果我们忽略原始类型,则变量是 never 对象,而对象永远不能是变量.所有对象总是神奇地在其他地方". (例如"GC堆"),并且您只能通过类似指针的句柄来处理它们.在Java中,类型为T变量始终是对类型为T的实际对象的引用,该对象位于其他地方.像C ++中一样,变量的作用域也一样,但是所有Java 对象的生存期都是不确定的,并且只能保证超出对给定对象的所有引用的生存期.

In Java, if we ignore the primitive types, variables are never objects, and objects can never be variables. All objects are always "magically somewhere else" (e.g. "the GC heap"), and you can only ever handle them through pointer-like handles. In Java, a variable of type T is always a reference to the actual object of type T, which lives somewhere else. Variables are also scoped, like in C++, but the lifetime of all Java objects is indeterminate, and only guaranteed to ex­tend beyond the lifetime of all references to a given object.

(对于内置的值"类型类型(如int),情况有所不同,可以作为变量的类型出现,而实际上不能是动态分配的.)

(The situation is different for built-in, "value"-type types like int, which can occur as the type of variables, and in fact cannot be allocated dynamically.)

在这方面,我认为PHP与Java类似.

I think PHP is similar to Java in that regard.

这篇关于当我在PHP中实例化一个类时,是否获得了指向该对象的指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆