Wednesday, November 11, 2020

PHP | get_object_vars() Function

PHP get_object_vars() Function


Hello dear readers! welcome back to another edition of our tutorial on PHP. In this tutorial guide, we are going to be discussing about the get_object_vars() Function in PHP.

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

RECOMMENDED POST: PHP | easter_date() Function

Parameter Details

Sr.NoParameter & Description
1

object

An object instance.


Return Value

It 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 rounding up for this tutorial post. In our next tutorial guide, we are going to be discussing about the PHP get_parent_class() Function.

Feel free to ask your questions where necessary and i 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.
Share:

0 comments:

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.