Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting a PHP array to class variables

Simple question, how do I convert an associative array to variables in a class? I know there is casting to do an (object) $myarray or whatever it is, but that will create a new stdClass and doesn't help me much. Are there any easy one or two line methods to make each $key => $value pair in my array into a $key = $value variable for my class? I don't find it very logical to use a foreach loop for this, I'd be better off just converting it to a stdClass and storing that in a variable, wouldn't I?

class MyClass {     var $myvar; // I want variables like this, so they can be references as $this->myvar     function __construct($myarray) {         // a function to put my array into variables     } } 
like image 595
animuson Avatar asked Apr 26 '10 17:04

animuson


People also ask

How do I turn an array into a stdClass object?

To convert an array into the object, stdClass() is used. The stdClass() is an empty class, which is used to cast other types to object. If an object is converted to object, its not modified. But, if object type is converted/type-casted an instance of stdClass is created, if it is not NULL.

How do you declare a class variable in PHP?

The var keyword in PHP is used to declare a property or variable of class which is public by default. The var keyword is same as public when declaring variables or property of a class.

Can I echo an array in PHP?

Use print_r() Function to Echo or Print an Array in PHP It stores the output of print_r() function. If its value is True , then the function will return the value which is supposed to print.

What does => mean in PHP array?

It means assign the key to $user and the variable to $pass. When you assign an array, you do it like this. $array = array("key" => "value"); It uses the same symbol for processing arrays in foreach statements. The '=>' links the key and the value.

How to convert array to variables in PHP?

This PHP extract function uses array keys as variable names and values as variable values. So using this extract function, we can print arrays by calling a variable name. From this example you will also see how to php array extract specific keys. It's a great feature of PHP. We can convert array to variables in PHP using this extract function.

How to convert array to stdClass in one line of PHP?

Using a type casting method you can quickly carry out an array to StdClass conversion in one line of code. Alternatively, you can harness the power of PHP’s encoding functions such as json_encode and json_decode.

How to extract specific keys from an array in PHP?

You know that PHP extract () function imports variables into the local symbol table from an array. This PHP extract function uses array keys as variable names and values as variable values. So using this extract function, we can print arrays by calling a variable name. From this example you will also see how to php array extract specific keys.

How to convert an array to a stdClass in JavaScript?

With the array, we first parse it into a JSON string with json_encode and finally, decode the string with the json_decode function. And again, we can utilise the object style syntax to access the values via their relative keys. And that is it, two super slick one-liners to convert an array into a fully operable StdClass object.


1 Answers

This simple code should work:

<?php    class MyClass {     public function __construct(Array $properties=array()){       foreach($properties as $key => $value){         $this->{$key} = $value;       }     }   }  ?> 

Example usage

$foo = new MyClass(array("hello" => "world")); $foo->hello // => "world" 

Alternatively, this might be a better approach

<?php    class MyClass {      private $_data;      public function __construct(Array $properties=array()){       $this->_data = $properties;     }      // magic methods!     public function __set($property, $value){       return $this->_data[$property] = $value;     }      public function __get($property){       return array_key_exists($property, $this->_data)         ? $this->_data[$property]         : null       ;     }   }  ?> 

Usage is the same

// init $foo = new MyClass(array("hello" => "world")); $foo->hello;          // => "world"  // set: this calls __set() $foo->invader = "zim";  // get: this calls __get() $foo->invader;       // => "zim"  // attempt to get a data[key] that isn't set $foo->invalid;       // => null 
like image 60
maček Avatar answered Sep 21 '22 21:09

maček