80855

How can we manipulate std class in php?

Question:

Is it possible to add elements to stdclasses like we do for arrays?

Array ( [0] => item 1 [1] => item 2 ) Stdclass ( [0] => item 1 [1] => item 2 )

Is it generally harder to manipulate objects as compared to arrays? Since we have lots of array functions to make use of etc.

Answer1:

Sure, you can add new elements...

$Object = new StdClass(); $Object->item1 = 1; $Object->item2 = 2;

If you want to iterate object as array you should use PHP SPL <a href="http://php.net/manual/en/class.arrayiterator.php" rel="nofollow">ArrayIterator</a> or <a href="http://www.php.net/manual/en/class.recursivearrayiterator.php" rel="nofollow">RecursiveArrayIterator</a>.

Also you can use typecasting to move from array to object and back...

$array = array('item1', 'item2'); $Object = (object)$array; var_dump($object); $array = (array)$Object; var_dump($array);

Answer2:

Yes, but not numerically indexed.

$a = new stdclass(); $a->foo = 'item 1'; $a->bar = 'item 2'; $a->goobar = array('item1', 'item2'); var_dump($a);

If you want more complex scenarios, look at <a href="http://php.net/ArrayAccess" rel="nofollow">ArrayAccess</a> and <a href="http://php.net/ArrayObject" rel="nofollow">ArrayObject</a>.

Answer3:

Well, the <a href="http://www.php.net/manual/en/reserved.classes.php" rel="nofollow">documentation</a> states:

<blockquote>

Created by typecasting to object.

</blockquote>

As such, you can cast back and forth between (array) and (object) to work stdClass instances with array functions.

Answer4:

It's no harder to manipulate. All I would say is things like count and array_search don't work on an object.

Array and objects both have their benefits it depends on what you are trying to achieve.

<a href="http://php.net" rel="nofollow">http://php.net</a> documentation will state what data types can be passed to what functions.

Recommend

  • How to convert bool to int in MySql
  • Why does my test of sign always report “negative”?
  • correct method to get the unsigned char pointer length
  • How to cast variable to array
  • Undefined property: Illuminate\\Queue\\Jobs\\BeanstalkdJob:: $name
  • Entity framework, referential integrity constraint violation occurred error on updating entity from
  • Specific pathing to find XML elements using minidom in Python
  • Determine what is the level of chlid php
  • Remove property by value from object
  • How to add custom images as UIApplicationShortcutIcon for UIApplicationShortcutItem?
  • How to invoke ActionListener for MenuItem in primefaces
  • Twitter OAuth: users/search works but search/tweets does not
  • Changing the color of an image link on hover
  • Get sibling of Vaadin Tree Item?
  • Why am I getting an Array of an Array with Doctrine 2 & Symfony 2 Data Transformer?
  • Crafting a LINQ based solution to determine if a set of predicates are satisfied for a pair of colle
  • inserting items in list in mongodb document
  • PHP file_exists() anomaly
  • how to remove a div with same ids but display='block' and display='none' in JAVa
  • What is this strange character in chrome's resource css viewer?
  • testing a POST using phpunit in laravel 4
  • Is there any way to call saveCurrentTurnWithMatchData without sending a push notification?
  • Angular2 - Template reference inside NgSwitch
  • How to use function wrapper in mustache.php?
  • msbuild create itemgroup from property group
  • Updating both a ConcurrentHashMap and an AtomicInteger safely
  • How to rebase a series of branches?
  • output of program is not same as passed argument
  • Does CUDA 5 support STL or THRUST inside the device code?
  • When should I choose bucket sort over other sorting algorithms?
  • Statically linking a C++ library to a C# process using CLI or any other way
  • Hazelcast - OperationTimeoutException
  • Upload files with Ajax and Jquery
  • To display the title for the current loaction in map in iphone
  • Why winpcap requires both .lib and .dll to run?
  • Akka Routing: Reply's send to router ends up as dead letters
  • AT Commands to Send SMS not working in Windows 8.1
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • How do I configure my settings file to work with unit tests?
  • Is it possible to post an object from jquery to bottle.py?