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. How d开发者_开发知识库o we do this in PHP ?
Regards, Mithun
Generic objects in PHP are instances of stdClass
. However it is not a base class, meaning classes don't inherit from it unless you specify extends stdClass
in the class declaration.
Typecasting something to (object)
in PHP yields a 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. 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, e.g. with new
.
As someone with both php and Java experience, I can say that there is no equivalent in php to Java's object. In Java every object extends Object class, in php a class you make does not extend anything by default. The Java's Object has some convenient methods like toString(), hashCode(), getClass() and a few more that would not be relevant to php.
I like these standard methods in Java, they are very convenient for debugging and logging, so I miss then in php. That's why I usually create my own base class in php and make every class extend it. Then it becomes easy to log and debug, you can just $logger->log($obj); and it will use magic __toString(), dumping at least basic info about the object.
The bottom line is that you can make your own base class in php and then just make every class extend it.
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).
Note that you can only typecast to stdClass
not any other classes, e.g. this will work
$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. If the value was NULL, the new instance will be empty. 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:
- Cast the current object ($this) to a descendent class
Ciaran answer is helpful,
Despite what the other two answers say, stdClass is not the base class for objects in 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. I don't believe there's a concept of a base object in PHP
Since PHP does not have type declarations, there is no need for a global base class. There isn't really anything to do: just declare variables and use them.
精彩评论