본문 바로가기

StackOverflow

[PHP] PHP에서 stdClass가 무엇인가요?

http://stackoverflow.com/questions/931407/what-is-stdclass-in-php


Q: PHP에서 stdClass가 무엇인가요?

stdClass 가 무엇인지 정의를 좀 해주세요.

(질문자: Keira Nighly)


A: stdClass는 PHP에서 일반적으로 쓰이는, 빈 클래스입니다. Java 의 object나 Python의 object와 유사합니다. (Ciaran 님이 지적해 주신대로, stdClass가 모든 클래스의 base class인 것은 아닙니다)

익명 오브젝트를 만들거나, 동적인 속성을 지정하거나 할 때 쓰입니다.

Dynamic Properties in PHP and StdClass 를 참조하세요.

(답변자:Alex Martelli)



A: 그냥 일반적인 '비어있는' 클래스이고, 다른 타입을 object로 캐스팅 할 때 쓰입니다. 다른 답변에 stdClass가 object의 base class라고 되어 있는데, 아닙니다. 간단히 예를 들어보면


class Foo{}

$foo = new Foo();

echo ($foo instanceof stdClass)?'Y':'N';

// 결과 값은 'N', 즉 class는 stdClass의 인스턴스가 아니며, stdClass는 class의 base class가 아님.


PHP에 base object 개념이 있다고는 전 믿지 않습니다.

(답변자: Ciaran McNulty)



_

Please define what stdClass is.

shareeditflag

protected by Amal Murali Dec 1 '13 at 4:57

This question is protected to prevent "thanks!", "me too!", or spam answers by new users. To answer it, you must have earned at least 10 reputation on this site.


_

452down voteaccepted

stdClass is PHP's generic empty class, kind of like Object in Java or object in Python (Edit: but not actually used as universal base class; thanks @Ciaran for pointing this out).

It is useful for anonymous objects, dynamic properties, etc.

See Dynamic Properties in PHP and StdClass for example.

shareeditflag
_

stdClass is just a generic 'empty' class that's used when casting other types to objects. 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)?'Y':'N';
// outputs 'N'

I don't believe there's a concept of a base object in PHP

shareeditflag