1

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

3
  • Do you have any predefined properties within the class? Commented Sep 16, 2017 at 10:28
  • no i don't. @SahilGulati. active record class has only CRUD methods Commented Sep 16, 2017 at 10:31
  • For that you can try either of the answers posted. Commented Sep 16, 2017 at 10:32

3 Answers 3

2

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.
}
Sign up to request clarification or add additional context in comments.

Comments

2

You can find properties in an object with:

$properties = get_object_vars($book);

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

Comments

1

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();

?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.