Just in Chronicles

Life as a Voyage

Bug on __get() Method Occurs in PHP 5.x

Reference: Overloaded properties (__get)

One of improvements since PHP 5.x was introduced is that developers use the concept of object-oriented programming (OOP). However, PHP was not born for OOP, so still hundreds of OO related issues arise and developers complain them. One of complaints is the very __get($name) function called a "magic method".

In OO world, accessor (getter) and mutator (setter) are generally used to access property values of a class. For example in C# and VB:

   1: For C#:

   2: public class MyClass

   3: {

   4:     private string text = null;

   5:     public string MyText

   6:     {

   7:         get { return this.text; }

   8:         set { this.text = value; }

   9:     }

  10: }

   1: For VB:

   2: Public Class MyClass

   3:     Private text As String = Nothing

   4:     Public Property MyText() As String

   5:         Get

   6:             Return Me.text

   7:         End Get

   8:         Set(ByVal value As String)

   9:             Me.text = value

  10:         End Set

  11:     End Property

  12: End Class

In PHP 5.x, two magic methods, __get($name) and __set($name, $value) are used to imitate both accessor and mutator, respectively. The sample code is:

   1: <?php

   2: class Sample

   3: {

   4:     private $properties = array("ID" => null, "CssClass" => null);

   5:     private function __get($name) { return $this->properties[$name]; }

   6:     private function __set($name, $value) { $this->properties[$name] = $value; }

   7: }

   8:  

   9: $sample = new Sample();

  10: $sample->ID = "sampleID";

  11: $sample->CssClass = "sampleClass";

  12: ?>

  13:  

  14: <div id="<?php echo $sample->ID; ?>" class="<?php echo $sample->CssClass; ?>">

  15: This is the sample div area

  16: </div>

The post linked above argues that __get($name) function has a bug when property values of a class are accessed. Even though the class has no error, sometimes the bug happens and the error message is like:

Notice: Indirect modification of overloaded property sample::$ID has no effect in ../sample.php on line 14

This is because of the bug stated above. In order to wipe out this error, additional code lines should be set before accessing property values like:

   1: <?php

   2: class Sample

   3: {

   4:     private $properties = array("ID" => null, "CssClass" => null);

   5:     private function __get($name) { return $this->properties[$name]; }

   6:     private function __set($name, $value) { $this->properties[$name] = $value; }

   7: }

   8:  

   9: $sample = new Sample();

  10: $sample->ID = "sampleID";

  11: $sample->CssClass = "sampleClass";

  12: ?>

  13:  

  14: <?php

  15: $id = $sample->ID;

  16: $cssClass = $sample->CssClass;

  17: ?>

  18:  

  19: <div id="<?php echo ($id); ?>" class="<?php echo ($cssClass); ?>">

  20: This is the sample div area

  21: </div>

Like the line 15 and 16, before echoing the property values, it should be better to declare another variables to get the property. Then, it will not show the error message.


PHP 5.x 버전으로 넘어오면서 향상된 기능중 하나가 객체지향프로그램이 좀 더 원활하게 가능해졌다는 점이다. 여전히 PHP는 객체지향을 위한 언어로서는 부족한 점이 많지만 말이다. 그런데, 여전히 문제점들을 가지고 있다. PHP 사이트에서는 버그가 고쳐졌다고 하지만, 여전히 불평하는 개발자들이 넘쳐나고 있다. 그중 한가지가 바로 magic method들 중 하나인 __get($name) 함수이다.

일반적으로 클라스의 속성값에 액세스할 때 get{}, set{} 접근자를 사용한다. 이와 비슷한 흉내를 내도록 만든 것이 바로 이 __get($name), __set($name, $value) 함수들인데, 함수를 가지고 접근자 흉내를 내려니 여러모로 애로사항이 꽃피고 있다. 위의 링크에 걸린 내용은 바로 이 __get($name) 함수를 통해 클라스 속성값을 가져올때 생기는 버그에 대한 것이다. 분명히 소스코드 상에서 아무런 오류가 없을지라도 종종 위와 같은 에러메시지를 나타낸다. 이것은 바로 __get($name) 함수의 버그 때문에 생기는 것으로, 이 메시지를 없애려면 조금 번거롭지만 추가적인 작업을 해줘야 한다.

만약 위 코드의 echo 부분에서 에러가 생긴다면, 저렇게 직접 데이타를 클라스 오브젝트 안에서 가져오기 보다 $id = $sample->ID; 형태로 일단 받아놓고 $idecho로 내보내면 에러메시지가 생기지 않는다.

이게 뭔 뻘짓이여… PHP는 아무리 봐도 정이 잘 안가!!

Advertisements

Written by Justin Yoo

26/05/2009 at 05:44

2 Responses

Subscribe to comments with RSS.

  1. me neither,,, Thats why i chose java, jsp all that for my final project which i still working on for uni, while the others chose php, very likly C#.
    I found that you shifted to here from daum blog.
    Thx u for you keep updating in regards to IT,

    -Mia

    mia choi in melbourn

    11/06/2009 at 13:49

    • Yes, I moved to here. I have no experience at Java but I’m sure it would be pretty similar to C#. I’ll keep updating my old posts to here as many as I can.

      Justin

      11/06/2009 at 15:27


Comments are closed.