Run
Run

Reputation: 57176

PHP class: how to a make method's data to become the class properties?

How can I turn the data from a class method into the properties of that class? Is it possible?

For instance, the article below class only has one property - $var1,

class article
{
    public $var1 = "var 1";
    public function __construct() 
    {

    }

    public function getRow() 
    {
        $array = array(
            "article_id" => 1,
            "url"       => "home",
            "title"     => "Home",
            "content"   => "bla bla"
        );

        return (object)$array;
    }
}

To get $this properties,

$article = new article();
print_r($article->var1); // var 1

To get $this method,

$row = $article->getRow();

To get $this method's data,

print_r($row->title); // Home

It works fine in that way, but how if I want to make/ move this dat**a below to the **class's properties,

            "article_id" => 1,
            "url"       => "home",
            "title"     => "Home",
            "content"   => "bla bla"

So I can just call the data like this,

$article = new article();
print_r($article->title); // Home

Is it possible?

Upvotes: 0

Views: 62

Answers (2)

Ivan Yonkov
Ivan Yonkov

Reputation: 7034

You would need to use the magic __set() method to create properties that do not exists. Afterwards move the object return from the method to simple property assignation

class article

{
    public $var1 = "var 1";
    public function __construct() 
    {
        $this->getRow();
    }

    public function getRow() 
    {
        $this->article_id = 1;
        $this->url = 'home';
        $this->title = "Home";
        $this->content = 'bla bla';
    }

    public function __set($name, $value) {
        $this->$name = $value;
    }
}

$article = new article();
echo $article->title; // prints Home

If you want to save your current logic (you said move, but to be sure, you don't want to break your getRow() logic), you can move the assignation in another method (or in the constructor).

class article

{
    public $var1 = "var 1";
    public function __construct() 
    {
        foreach ($this->getRow() as $name => $value) {
            $this->$name = $value;
        }
    }

Also if you don't won't anything different that the properties from getRow() to be magically used, you can unset any other assignation in your __set() method:

$rows = (array)$this->getRow();
if (!array_key_exists($name, $rows)) {
    unset($this->$name);
}

Upvotes: 1

sergio
sergio

Reputation: 5260

One of possible way is to set this properties like this:

class article
{
    public function __construct() 
    {
        $array = array(
            "article_id" => 1,
            "url"       => "home",
            "title"     => "Home",
            "content"   => "bla bla"
        );
     foreach($array as $key => $value){
        $this->{$key} = $value;
      }
   }
}

And now you can get :

$article = new article();
print_r($article->title);  //Home

Upvotes: 1

Related Questions