简体   繁体   English

什么是PHP相当于Java的对象类

[英]What is PHP equivalent of Java's object class

In java we have Object type which can be used to cast to specific class type. 在java中,我们有Object类型,可用于转换为特定的类类型。 How do we do this in PHP ? 我们如何在PHP中执行此操作?

Regards, Mithun 此致,Mithun

Generic objects in PHP are instances of stdClass . PHP中的通用对象是stdClass实例。 However it is not a base class, meaning classes don't inherit from it unless you specify extends stdClass in the class declaration. 但是它不是基类,这意味着除非在类声明中指定extends stdClass ,否则类不会继承它。

Typecasting something to (object) in PHP yields a stdClass . 在PHP中对(object)进行类型转换会产生一个stdClass For example: 例如:

$a = array('foo' => 'bar');
$o = (object) $a;
var_dump($o instanceof stdClass); // bool(true)
var_dump($o->foo); // string(3) "bar"

In PHP there is no concept of upcasting and downcasting. 在PHP中,没有向上转换和向下转换的概念。 You can type hint for superclasses or interfaces but that's about it. 您可以为超类或接口键入提示,但这是关于它的。 An object is always recognized as an instance of whatever class you construct it as, eg with new . 对象始终被识别为您构造它的任何类的实例,例如new

As someone with both php and Java experience, I can say that there is no equivalent in php to Java's object. 作为拥有php和Java经验的人,我可以说php中没有与Java对象相当的东西。 In Java every object extends Object class, in php a class you make does not extend anything by default. 在Java中,每个对象都扩展了Object类,在php中,您创建的类默认不扩展任何内容。 The Java's Object has some convenient methods like toString(), hashCode(), getClass() and a few more that would not be relevant to php. Java的Object有一些方便的方法,比如toString(),hashCode(),getClass()以及一些与php无关的方法。

I like these standard methods in Java, they are very convenient for debugging and logging, so I miss then in php. 我喜欢Java中的这些标准方法,它们非常便于调试和记录,所以我很想念它。 That's why I usually create my own base class in php and make every class extend it. 这就是为什么我通常在php中创建自己的基类并让每个类扩展它。 Then it becomes easy to log and debug, you can just $logger->log($obj); 然后它变得很容易记录和调试,你只需$ logger-> log($ obj); and it will use magic __toString(), dumping at least basic info about the object. 它将使用magic __toString(),至少转储有关该对象的基本信息。

The bottom line is that you can make your own base class in php and then just make every class extend it. 最重要的是,您可以在php中创建自己的基类,然后让每个类扩展它。

My usual base class: 我通常的基类:

/**
 * Base class for all custom objects
 * well, not really all, but
 * many of them, especially
 * the String and Array objects
 *
 * @author Dmitri Snytkine
 *
 */
class BaseObject
{

    /**
     * Get unique hash code for the object
     * This code uniquely identifies an object,
     * even if 2 objects are of the same class
     * and have exactly the same properties, they still
     * are uniquely identified by php
     *
     * @return string
     */
    public function hashCode()
    {
        return spl_object_hash($this);
    }

    /**
     * Getter of the class name
     * @return string the class name of this object
     */
    public function getClass()
    {
        return get_class($this);
    }

    /**
     * Outputs the name and uniqe code of this object
     * @return string
     */
    public function __toString()
    {
        return 'object of type: '.$this->getClass().' hashCode: '.$this->hashCode();
    }

}

This would be stdClass (which is not a base class ftm). 这将是stdClass (它不是基类ftm)。

Note that you can only typecast to stdClass not any other classes, eg this will work 请注意,您只能对stdClass 类型转换而不是任何其他类,例如,这将起作用

$obj = (object) array( 'foo' => 'bar' );

but not 但不是

$observer = (Observer) new Subject;

Quoting the manual: 引用手册:

If an object is converted to an object, it is not modified. 如果将对象转换为对象,则不会对其进行修改。 If a value of any other type is converted to an object, a new instance of the stdClass built-in class is created. 如果将任何其他类型的值转换为对象,则会创建stdClass内置类的新实例。 If the value was NULL, the new instance will be empty. 如果值为NULL,则新实例将为空。 Arrays convert to an object with properties named by keys, and corresponding values. 数组转换为具有按键和相应值命名的属性的对象。 For any other value, a member variable named scalar will contain the value. 对于任何其他值,名为标量的成员变量将包含该值。

Well, unless you are willing to utilize black magic and unreliable hacks, such as those given in: 好吧,除非你愿意利用黑魔法和不可靠的黑客攻击,例如:

Ciaran answer is helpful, Ciaran的回答很有帮助,

Despite what the other two answers say, stdClass is not the base class for objects in PHP. 尽管其他两个答案都说,stdClass不是PHP中对象的基类。 This can be demonstrated fairly easily: 这可以很容易地证明:

class Foo{} 
$foo = new Foo(); 
echo ($foo instanceof stdClass)?'Yes':'No';

it outputs 'N' stdClass is instead just a generic 'empty' class that's used when casting other types to objects. 它输出'N'stdClass只是一个通用的'空'类,在将其他类型转换为对象时使用。 I don't believe there's a concept of a base object in PHP 我不相信PHP中存在基础对象的概念

Since PHP does not have type declarations, there is no need for a global base class. 由于PHP没有类型声明,因此不需要全局基类。 There isn't really anything to do: just declare variables and use them. 没有什么可做的:只需声明变量并使用它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM