PHP는 객체지향 부분에 관련 매직 메소드가 많이 있습니다. 이러한 메소드는 객체지향 구현에 대한 편의성을 제공합니다. 이 글에서는 매직 메소드를 자세히 소개합니다.
대부분의 클래스에는 생성자라는 특별한 메서드가 있습니다. 객체가 생성되면 일반적으로 유용한 초기화 작업을 수행하는 데 사용되는 생성자를 자동으로 호출합니다.
생성자의 선언은 이름이 두 개의 밑줄 __construct()여야 한다는 점을 제외하면 다른 작업의 선언과 동일합니다. 이는 PHP5의 변경 사항입니다. PHP4 버전에서는 생성자의 이름이 클래스 이름과 동일해야 합니다. 이전 버전과의 호환성을 위해 클래스에 __construct()라는 메서드가 없으면 PHP는 클래스와 동일한 이름을 가진 메서드를 검색합니다.
void __construct ([ <span style="color: #0000ff;">mixed</span> <span style="color: #800080;">$args</span> [, $... ]] )
하위 클래스에 생성자가 정의된 경우 해당 상위 클래스의 생성자는 암시적으로 호출되지 않습니다. 상위 클래스의 생성자를 실행하려면 하위 클래스의 생성자에서 parent::__construct()를 호출해야 합니다. 하위 클래스가 생성자를 정의하지 않으면 일반 클래스 메서드와 마찬가지로 상위 클래스에서 상속됩니다(private으로 정의되지 않은 경우)
<?<span style="color: #000000;">php </span><span style="color: #0000ff;">class</span><span style="color: #000000;"> BaseClass { </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> __construct() { </span><span style="color: #0000ff;">print</span> "In BaseClass constructor\n"<span style="color: #000000;">; } } </span><span style="color: #0000ff;">class</span> SubClass <span style="color: #0000ff;">extends</span><span style="color: #000000;"> BaseClass { </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> __construct() { parent</span>::<span style="color: #000000;">__construct(); </span><span style="color: #0000ff;">print</span> "In SubClass constructor\n"<span style="color: #000000;">; } } </span><span style="color: #0000ff;">class</span> OtherSubClass <span style="color: #0000ff;">extends</span><span style="color: #000000;"> BaseClass { } </span><span style="color: #008000;">//</span><span style="color: #008000;"> In BaseClass constructor</span> <span style="color: #800080;">$obj</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> BaseClass(); </span><span style="color: #008000;">//</span><span style="color: #008000;"> In BaseClass constructor // In SubClass constructor</span> <span style="color: #800080;">$obj</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> SubClass(); </span><span style="color: #008000;">//</span><span style="color: #008000;"> In BaseClass constructor</span> <span style="color: #800080;">$obj</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> OtherSubClass(); </span>?>
시공방식의 반대는 파괴방식입니다. 소멸자 메소드는 PHP5에 새로 추가된 내용입니다. PHP4에는 소멸자 메소드가 없습니다. 소멸자 메서드는 객체가 소멸되기 전에 자동으로 호출되는 메서드로, 주로 파일 닫기, 결과 세트 해제 등과 같은 일부 특정 작업을 수행합니다.
생성자 메서드와 유사하게 클래스의 소멸자 메서드 이름은 두 개의 밑줄(_ _destruct())이어야 합니다. 소멸자는 어떤 매개변수도 취할 수 없습니다
<?<span style="color: #000000;">php </span><span style="color: #0000ff;">class</span><span style="color: #000000;"> MyDestructableClass { </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> __construct() { </span><span style="color: #0000ff;">print</span> "In constructor\n"<span style="color: #000000;">; </span><span style="color: #800080;">$this</span>->name = "MyDestructableClass"<span style="color: #000000;">; } </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> __destruct() { </span><span style="color: #0000ff;">print</span> "Destroying " . <span style="color: #800080;">$this</span>->name . "\n"<span style="color: #000000;">; } } </span><span style="color: #008000;">//</span><span style="color: #008000;">In constructor Destroying MyDestructableClass</span> <span style="color: #800080;">$obj</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> MyDestructableClass(); </span>?>
get()
액세스할 수 없는 속성(protected, private)을 읽을 때 __get()이 호출되고 속성 이름이 첫 번째 매개변수(문자열)로 이 메서드에 전달됩니다.
<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">mixed</span> __get ( <span style="color: #0000ff;">string</span> <span style="color: #800080;">$name</span> )
<?<span style="color: #000000;">php </span><span style="color: #0000ff;">class</span><span style="color: #000000;"> demo{ </span><span style="color: #0000ff;">protected</span> <span style="color: #800080;">$protected</span> = 1<span style="color: #000000;">; </span><span style="color: #0000ff;">public</span> <span style="color: #800080;">$public</span> = 2<span style="color: #000000;">; </span><span style="color: #0000ff;">private</span> <span style="color: #800080;">$private</span> = 3<span style="color: #000000;">; </span><span style="color: #0000ff;">function</span> __get(<span style="color: #800080;">$name</span><span style="color: #000000;">){ </span><span style="color: #0000ff;">echo</span> "111{<span style="color: #800080;">$name</span>}111<br>"<span style="color: #000000;">; } } </span><span style="color: #800080;">$d1</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> demo; </span><span style="color: #800080;">$d1</span>-><span style="color: #0000ff;">protected</span>;<span style="color: #008000;">//</span><span style="color: #008000;">111protected111</span> <span style="color: #800080;">$d1</span>-><span style="color: #0000ff;">public</span><span style="color: #000000;">; </span><span style="color: #800080;">$d1</span>-><span style="color: #0000ff;">private</span>;<span style="color: #008000;">//</span><span style="color: #008000;">111private111</span> ?>
세트()
접근할 수 없는 속성(protected, private)에 값을 할당할 때 __set()가 호출되며, 속성 이름은 첫 번째 매개변수(문자열)로 값이 두 번째 매개변수(혼합)로 이 메서드에 전달됩니다. )
<span style="color: #0000ff;">public</span> void __set ( <span style="color: #0000ff;">string</span> <span style="color: #800080;">$name</span> , <span style="color: #0000ff;">mixed</span> <span style="color: #800080;">$value</span> )
<?<span style="color: #000000;">php </span><span style="color: #0000ff;">class</span><span style="color: #000000;"> demo{ </span><span style="color: #0000ff;">protected</span> <span style="color: #800080;">$protected</span> = 1<span style="color: #000000;">; </span><span style="color: #0000ff;">public</span> <span style="color: #800080;">$public</span> = 2<span style="color: #000000;">; </span><span style="color: #0000ff;">private</span> <span style="color: #800080;">$private</span> = 3<span style="color: #000000;">; </span><span style="color: #0000ff;">function</span> __set(<span style="color: #800080;">$name</span>,<span style="color: #800080;">$value</span><span style="color: #000000;">){ </span><span style="color: #0000ff;">echo</span> "0{<span style="color: #800080;">$name</span>}0{<span style="color: #800080;">$value</span>}<br>"<span style="color: #000000;">; } } </span><span style="color: #800080;">$d1</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> demo; </span><span style="color: #800080;">$d1</span>-><span style="color: #0000ff;">protected</span> = '1';<span style="color: #008000;">//</span><span style="color: #008000;">0protected01</span> <span style="color: #800080;">$d1</span>-><span style="color: #0000ff;">public</span> = '2'<span style="color: #000000;">; </span><span style="color: #800080;">$d1</span>-><span style="color: #0000ff;">private</span> = '3';<span style="color: #008000;">//</span><span style="color: #008000;">0private03</span> ?>
isset()
액세스할 수 없는 속성(보호, 개인)에 대해 isset() 또는 empty()가 호출되면 __isset()가 호출됩니다.
<span style="color: #0000ff;">public</span> bool __isset ( <span style="color: #0000ff;">string</span> <span style="color: #800080;">$name</span> )
<?<span style="color: #000000;">php </span><span style="color: #0000ff;">class</span><span style="color: #000000;"> demo{ </span><span style="color: #0000ff;">protected</span> <span style="color: #800080;">$protected</span> = 1<span style="color: #000000;">; </span><span style="color: #0000ff;">public</span> <span style="color: #800080;">$public</span> = 2<span style="color: #000000;">; </span><span style="color: #0000ff;">private</span> <span style="color: #800080;">$private</span> = 3<span style="color: #000000;">; </span><span style="color: #0000ff;">function</span> __isset(<span style="color: #800080;">$name</span><span style="color: #000000;">){ </span><span style="color: #0000ff;">echo</span> "0{<span style="color: #800080;">$name</span>}0<br>"<span style="color: #000000;">; } } </span><span style="color: #800080;">$d1</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> demo; </span><span style="color: #0000ff;">empty</span>(<span style="color: #800080;">$d1</span>-><span style="color: #0000ff;">protected</span>);<span style="color: #008000;">//</span><span style="color: #008000;">0protected0</span> <span style="color: #0000ff;">empty</span>(<span style="color: #800080;">$d1</span>-><span style="color: #0000ff;">public</span><span style="color: #000000;">); </span><span style="color: #0000ff;">empty</span>(<span style="color: #800080;">$d1</span>-><span style="color: #0000ff;">private</span>);<span style="color: #008000;">//</span><span style="color: #008000;">0private0</span> ?>
설정 해제()
액세스할 수 없는 속성(보호, 비공개)에 대해 unset()을 호출하면 __unset()이 호출됩니다.
<span style="color: #0000ff;">public</span> void __unset ( <span style="color: #0000ff;">string</span> <span style="color: #800080;">$name</span> )
<?<span style="color: #000000;">php </span><span style="color: #0000ff;">class</span><span style="color: #000000;"> demo{ </span><span style="color: #0000ff;">protected</span> <span style="color: #800080;">$protected</span> = 1<span style="color: #000000;">; </span><span style="color: #0000ff;">public</span> <span style="color: #800080;">$public</span> = 2<span style="color: #000000;">; </span><span style="color: #0000ff;">private</span> <span style="color: #800080;">$private</span> = 3<span style="color: #000000;">; </span><span style="color: #0000ff;">function</span> __unset(<span style="color: #800080;">$name</span><span style="color: #000000;">){ </span><span style="color: #0000ff;">echo</span> "0{<span style="color: #800080;">$name</span>}0<br>"<span style="color: #000000;">; } } </span><span style="color: #800080;">$d1</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> demo; </span><span style="color: #0000ff;">unset</span>(<span style="color: #800080;">$d1</span>-><span style="color: #0000ff;">protected</span>);<span style="color: #008000;">//</span><span style="color: #008000;">0protected0</span> <span style="color: #0000ff;">unset</span>(<span style="color: #800080;">$d1</span>-><span style="color: #0000ff;">public</span><span style="color: #000000;">); </span><span style="color: #0000ff;">unset</span>(<span style="color: #800080;">$d1</span>-><span style="color: #0000ff;">private</span>);<span style="color: #008000;">//</span><span style="color: #008000;">0private0</span> ?>
클론()
객체가 복제되면 clone() 메서드가 자동으로 호출됩니다. 이 메서드에는 매개 변수가 필요하지 않습니다. 이 메서드를 사용하면 복제된 복사본을 다시 초기화할 수 있습니다.
clone() 메서드는 this와 that이라는 두 개체에 대한 참조를 자동으로 포함합니다. 이는 복사 개체에 대한 참조이고, 이는 원본 개체에 대한 참조입니다.
<?<span style="color: #000000;">php </span><span style="color: #0000ff;">class</span><span style="color: #000000;"> Person{ </span><span style="color: #0000ff;">private</span> <span style="color: #800080;">$name</span><span style="color: #000000;">; </span><span style="color: #0000ff;">private</span> <span style="color: #800080;">$sex</span><span style="color: #000000;">; </span><span style="color: #0000ff;">private</span> <span style="color: #800080;">$age</span><span style="color: #000000;">; </span><span style="color: #0000ff;">function</span> __construct(<span style="color: #800080;">$name</span>="",<span style="color: #800080;">$sex</span>="",<span style="color: #800080;">$age</span>=1<span style="color: #000000;">){ </span><span style="color: #800080;">$this</span>->name= <span style="color: #800080;">$name</span><span style="color: #000000;">; </span><span style="color: #800080;">$this</span>->sex = <span style="color: #800080;">$sex</span><span style="color: #000000;">; </span><span style="color: #800080;">$this</span>->age = <span style="color: #800080;">$age</span><span style="color: #000000;">; } </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> __clone(){ </span><span style="color: #800080;">$this</span>->name = <span style="color: #800080;">$this</span>->name."的副本"<span style="color: #000000;">; } </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> say(){ </span><span style="color: #0000ff;">echo</span> "我的名字:" .<span style="color: #800080;">$this</span>->name.",性别:".<span style="color: #800080;">$this</span>->sex.",年龄:".<span style="color: #800080;">$this</span>->age."<br>"<span style="color: #000000;">; } } </span><span style="color: #800080;">$p1</span> = <span style="color: #0000ff;">new</span> Person('张三','男','20'<span style="color: #000000;">); </span><span style="color: #800080;">$p2</span> = <span style="color: #0000ff;">clone</span> <span style="color: #800080;">$p1</span><span style="color: #000000;">; </span><span style="color: #800080;">$p1</span>->say();<span style="color: #008000;">//</span><span style="color: #008000;">我的名字:张三,性别:男,年龄:20</span> <span style="color: #800080;">$p2</span>->say();<span style="color: #008000;">//</span><span style="color: #008000;">我的名字:张三的副本,性别:男,年龄:20</span> ?>
toString()
__toString() 메서드는 클래스가 문자열로 처리될 때 어떻게 응답해야 하는지에 사용됩니다. 객체의 문자열 표현을 직접 얻을 때 가장 편리한 방법입니다. 객체 참조 출력
<?<span style="color: #000000;">php </span><span style="color: #0000ff;">class</span><span style="color: #000000;"> TestClass { </span><span style="color: #0000ff;">public</span> <span style="color: #800080;">$foo</span><span style="color: #000000;">; </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> __construct(<span style="color: #800080;">$foo</span><span style="color: #000000;">) { </span><span style="color: #800080;">$this</span>->foo = <span style="color: #800080;">$foo</span><span style="color: #000000;">; } </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> __toString() { </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$this</span>-><span style="color: #000000;">foo; } } </span><span style="color: #800080;">$class</span> = <span style="color: #0000ff;">new</span> TestClass('Hello'<span style="color: #000000;">); </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$class</span>;<span style="color: #008000;">//</span><span style="color: #008000;">Hello</span> ?>
전화()
객체에서 액세스할 수 없는 메서드가 호출되면 __call()이 호출됩니다.
callStatic()
정적 컨텍스트에서 액세스할 수 없는 메서드를 호출하면 __callStatic()이 호출됩니다.
<?<span style="color: #000000;">php </span><span style="color: #0000ff;">class</span><span style="color: #000000;"> MethodTest { </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> __call(<span style="color: #800080;">$name</span>, <span style="color: #800080;">$arguments</span><span style="color: #000000;">) { </span><span style="color: #0000ff;">echo</span> "Calling object method '<span style="color: #800080;">$name</span>' " . <span style="color: #008080;">implode</span>(', ', <span style="color: #800080;">$arguments</span>). "\n"<span style="color: #000000;">; } </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">function</span> __callStatic(<span style="color: #800080;">$name</span>, <span style="color: #800080;">$arguments</span><span style="color: #000000;">) { </span><span style="color: #0000ff;">echo</span> "Calling static method '<span style="color: #800080;">$name</span>' " . <span style="color: #008080;">implode</span>(', ', <span style="color: #800080;">$arguments</span>). "\n"<span style="color: #000000;">; } } </span><span style="color: #800080;">$obj</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> MethodTest; </span><span style="color: #008000;">//</span><span style="color: #008000;">Calling object method 'runTest' in object context</span> <span style="color: #800080;">$obj</span>->runTest('in object context'<span style="color: #000000;">); </span><span style="color: #008000;">//</span><span style="color: #008000;">Calling static method 'runTest' in static context</span> MethodTest::runTest('in static context'<span style="color: #000000;">); </span>?>
자동 로드()
PHP5에서는 아직 정의되지 않은 클래스를 사용하려고 할 때 자동으로 호출되는 __autoload() 함수를 정의할 수 있습니다. 이 함수를 호출하면 스크립트 엔진은 PHP가 오류로 인해 실패하기 전에 필요한 클래스를 로드할 수 있는 마지막 기회를 갖게 됩니다
<?<span style="color: #000000;">php </span><span style="color: #0000ff;">function</span> __autoload(<span style="color: #800080;">$class_name</span><span style="color: #000000;">) { </span><span style="color: #0000ff;">require_once</span> <span style="color: #800080;">$class_name</span> . '.php'<span style="color: #000000;">; } </span><span style="color: #800080;">$obj</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> MyClass1(); </span><span style="color: #800080;">$obj2</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> MyClass2(); </span>?>
sleep()
在调用serialize()函数将对象串行化时,检查类中是否存在一个魔术方法 __sleep()。如果存在,该方法会先被调用,然后才执行序列化操作。此功能可以用于清理对象,并返回一个包含对象中所有应被序列化的变量名称的数组。如果该方法未返回任何内容,则 NULL 被序列化,并产生一个 E_NOTICE 级别的错误
__sleep()函数不需要接受任何参数,但需要返回一个数组,在数组中包含需要串行化的属性。未被包含在数组中的属性将在串行化时被忽略。如果没有在类中声明__sleep()方法,对象中的所有属性都将被串行化
wakeup()
在调用unserialize()函数将对象反串行化对象时,则会自动调用对象中的__wakeup()方法,用来在二进制串重新组成一个对象时,为新对象中的成员属性重新初始化
wakeup()经常用在反序列化操作中,例如重新建立数据库连接,或执行其它初始化操作
<?<span style="color: #000000;">php </span><span style="color: #0000ff;">class</span><span style="color: #000000;"> Connection { </span><span style="color: #0000ff;">protected</span> <span style="color: #800080;">$link</span><span style="color: #000000;">; </span><span style="color: #0000ff;">private</span> <span style="color: #800080;">$server</span>, <span style="color: #800080;">$username</span>, <span style="color: #800080;">$password</span>, <span style="color: #800080;">$db</span><span style="color: #000000;">; </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> __construct(<span style="color: #800080;">$server</span>, <span style="color: #800080;">$username</span>, <span style="color: #800080;">$password</span>, <span style="color: #800080;">$db</span><span style="color: #000000;">) { </span><span style="color: #800080;">$this</span>->server = <span style="color: #800080;">$server</span><span style="color: #000000;">; </span><span style="color: #800080;">$this</span>->username = <span style="color: #800080;">$username</span><span style="color: #000000;">; </span><span style="color: #800080;">$this</span>->password = <span style="color: #800080;">$password</span><span style="color: #000000;">; </span><span style="color: #800080;">$this</span>->db = <span style="color: #800080;">$db</span><span style="color: #000000;">; </span><span style="color: #800080;">$this</span>-><span style="color: #000000;">connect(); } </span><span style="color: #0000ff;">private</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> connect() { </span><span style="color: #800080;">$this</span>->link = <span style="color: #008080;">mysql_connect</span>(<span style="color: #800080;">$this</span>->server, <span style="color: #800080;">$this</span>->username, <span style="color: #800080;">$this</span>-><span style="color: #000000;">password); </span><span style="color: #008080;">mysql_select_db</span>(<span style="color: #800080;">$this</span>->db, <span style="color: #800080;">$this</span>-><span style="color: #000000;">link); } </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> __sleep() { </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">array</span>('server', 'username', 'password', 'db'<span style="color: #000000;">); } </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> __wakeup() { </span><span style="color: #800080;">$this</span>-><span style="color: #000000;">connect(); } } </span>?>
invoke()
当尝试以调用函数的方式调用一个对象时,__invoke()方法会被自动调用
<?<span style="color: #000000;">php </span><span style="color: #0000ff;">class</span><span style="color: #000000;"> CallableClass { </span><span style="color: #0000ff;">function</span> __invoke(<span style="color: #800080;">$x</span><span style="color: #000000;">) { </span><span style="color: #008080;">var_dump</span>(<span style="color: #800080;">$x</span><span style="color: #000000;">); } } </span><span style="color: #800080;">$obj</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> CallableClass; </span><span style="color: #800080;">$obj</span>(5);<span style="color: #008000;">//</span><span style="color: #008000;">int(5)</span> <span style="color: #008080;">var_dump</span>(<span style="color: #008080;">is_callable</span>(<span style="color: #800080;">$obj</span>));<span style="color: #008000;">//</span><span style="color: #008000;">bool(true)</span> ?>
【补充】
set_state()
当调用var_export()导出类时,set_state()方法会被调用,本方法的唯一参数是一个数组,其中包含按 array('property' => value, ...) 格式排列的类属性
[注意]var_export()返回关于传递给该函数的变量的结构信息,它和var_dump()类似,不同的是其返回的表示是合法的PHP代码,也就是说,var_export返回的代码,可以直接当作php代码赋给一个变量。 而这个变量就会取得和被var_export一样的类型的值
<?<span style="color: #000000;">php </span><span style="color: #0000ff;">class</span><span style="color: #000000;"> A { </span><span style="color: #0000ff;">public</span> <span style="color: #800080;">$var1</span><span style="color: #000000;">; </span><span style="color: #0000ff;">public</span> <span style="color: #800080;">$var2</span><span style="color: #000000;">; </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">function</span> __set_state(<span style="color: #800080;">$an_array</span><span style="color: #000000;">) { </span><span style="color: #800080;">$obj</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> A; </span><span style="color: #800080;">$obj</span>->var1 = <span style="color: #800080;">$an_array</span>['var1'<span style="color: #000000;">]; </span><span style="color: #800080;">$obj</span>->var2 = <span style="color: #800080;">$an_array</span>['var2'<span style="color: #000000;">]; </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$obj</span><span style="color: #000000;">; } } </span><span style="color: #800080;">$a</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> A; </span><span style="color: #800080;">$a</span>->var1 = 5<span style="color: #000000;">; </span><span style="color: #800080;">$a</span>->var2 = 'foo'<span style="color: #000000;">; </span><span style="color: #0000ff;">eval</span>('$b = ' . <span style="color: #008080;">var_export</span>(<span style="color: #800080;">$a</span>, <span style="color: #0000ff;">true</span>) . ';'<span style="color: #000000;">); </span><span style="color: #008000;">/*</span><span style="color: #008000;"> object(A)[2] public 'var1' => int 5 public 'var2' => string 'foo' (length=3) </span><span style="color: #008000;">*/</span> <span style="color: #008080;">var_dump</span>(<span style="color: #800080;">$b</span><span style="color: #000000;">); </span>?>