www.webdeveloper.com
Results 1 to 4 of 4

Thread: Add limitation to php class

  1. #1
    Join Date
    Apr 2005
    Posts
    210

    Talking Add limitation to php class

    This is C++ class format in php style:
    Code:
    class My
    {
          var $name;
    }
    A variable must be defined before use in c++, i.e:
    $my = new My();
    $my->day ...


    This causes a compile error in C++: $day is not defined in class My ...
    But, php simply adds a variable $day to the class without any error.

    This is really no good, may I add limitation to php class to simulate C++ class in case of example above?

    Thanks.

    .

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,338
    You can use the __set() "magic method" to limit what can be done.
    PHP Code:
    <?php
    class Test
    {
        public 
    $foo;
        private 
    $bar;

        public function 
    __set($varName$value)
        {
            if( ! 
    property_exists($this$varName)) {
                throw new 
    Exception("Invalid property '$varName'");
            }
            else {
                throw new 
    Exception("Private property, keep off");
            }
        }
    }

    $test = new Test();
    $test->foo "This is okay";
    try {
        
    $test->bar "This should not be allowed.";
    }
    catch(
    Exception $e) {
        echo 
    "<pre>".print_r($e1)."</pre>";
    }
    try {
        
    $test->fubar "This won't work either.";
    }
    catch(
    Exception $e) {
        echo 
    "<pre>".print_r($e1)."</pre>";
    }
    (You can use the __get() magic method to access non-public variables, too, which in combination with the __set() above could make them effective read-only.)
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Apr 2005
    Posts
    210
    Hi NogDog,

    I never think there is a solution for my need.
    Your code is over magic!

    Does the checking function heavily slow down php execution?

    Thanks a lot.

    .

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,338
    I don't think the initial check to determine if the __set() method needs to be called at all should be slow, since that would be part of the underlying C code. If you actually get into the method itself, then you're in more or less "normal" PHP mode -- but since in this specific case we're throwing exceptions, ideally any developer would then adjust his/her client code to not do that. (sort of like a compilation error in C).
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles