We now have a youtube channel. Subscribe!

PHP | get_object_vars() Function

PHP get_object_vars() Function


Hello folks! welcome back to a new edition of our tutorial on PHP. In this tutorial guide, we are going to be studying about the PHP get_object_vars() Function.

The built-in PHP get_object_vars() Function gets the properties of the given object.

Syntax

Following below is the syntax to use this function -

get_object_vars ( $object);


Parameter Details

Sr.NoParameter & Description
1

object

An object instance.


Return Value

This PHP function returns an associative array of defined object properties for the specified object. If a property have not been assigned a value, it will be returned with a NULL value.

Example

Try out the below example -

<?php
   class Point2D {
      var $x, $y;
      var $label;
      
      function Point2D($x, $y) {
         $this->x = $x;
         $this->y = $y;
      }
      
      function setLabel($label) {
         $this->label = $label;
      }
      
      function getPoint() {
         return array("x" => $this->x, "y" => $this->y, "label" => $this->label);
      }
   }
   $p1 = new Point2D(1.233, 3.445);
   print_r(get_object_vars($p1));
   
   $p1->setLabel("point #1");
   print_r(get_object_vars($p1));
?> 

Output

When the above code is executed, it will produce the following result -

Array (
   [x] => 1.233
   [y] => 3.445
   [label] =>
)
Array (
   [x] => 1.233
   [y] => 3.445
   [label] => point #1
)


Alright guys! This is where we are going to be rounding up for this tutorial post. In our next tutorial, we are going to be discussing about the get_parent_class() Function.

Feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.

Thanks for reading and bye for now.

Post a Comment

Hello dear readers! Please kindly try your best to make sure your comments comply with our comment policy guidelines. You can visit our comment policy page to view these guidelines which are clearly stated. Thank you.
© 2023 ‧ WebDesignTutorialz. All rights reserved. Developed by Jago Desain