Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to get default properties of the given class in PHP?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 364
    Comment on it

    Hello friends,

    Today we learn how to get the properties of a class in PHP. In PHP, get_class_vars() function is used to get all the default properties of a particular class. It will return the result in the form of associated array. The properties that will display by the function will be public properties.

    Syntax:

    get_class_vars ( $class_name );

    $class_name in the above function is the name of the class. It's a required field.

    Let's take an example to understand it.

    <?php
       class myclass {
          var $var1;
          var $var2 = "xyz";
          var $var3 = 100;
          private $var4;
       }
    
       $class_vars = get_class_vars('myclass');
       
       foreach ($class_vars as $name => $value) {
          echo $name.' : '.$value ."<br>";
       }
    ?> 

    Result of above example is as follows:

    var1 :
    var2 : xyz
    var3 : 100

    var4 in the above example is the private properties and other are public. That's why only 3 properties are displayed in the output.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: