简体   繁体   中英

how can i access properties defined dynamically inside php class

trying to develop a class associated with table(like in frameworks). assume we have a class named Book

class Book
{  
  public function save()
  {
     ....
  }

 }

$book = new book;
$book->id = '1';
$book->name = 'some';
$book->save();

the problem is how can i access this dynamically created properties inside save() to save new record

You could do it that way (note that there are other solutions to this problem) :

public function save() {
    $properties = get_object_vars($this);
    print_r($properties);
    // do something with it.
}

You can find properties in an object with:

$properties = get_object_vars($book);

See: http://php.net/manual/en/function.get-object-vars.php

here is complete code that you should use:

 <?php class Book { public function save() { $vars = get_object_vars($this); var_dump($vars); } } $book = new book; $book->id = '1'; $book->name = 'some'; $book->save(); ?> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM