• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / PHP / php property_exists() – Check if Property Exists in Class or Object

php property_exists() – Check if Property Exists in Class or Object

March 17, 2022 Leave a Comment

In php, we can check if a property exists in a class or an object with the built-in php property_exists() function.

class exampleClass {
    public $id;
    private $name;
    static protected $multiplyNumbers;

    static function multiplyNumbers($a,$b) {
        return $a * $b;
    }
}

var_dump(property_exists('exampleClass', 'name'));  
var_dump(property_exists(new exampleClass, 'name')); 
var_dump(property_exists('exampleClass', 'multiplyNumbers'));  
var_dump(property_exists('exampleClass', 'other'));    

// Output:
bool(true)
bool(true)
bool(true)
bool(false)

When working with objects in our php programs, it is useful to be able to check if an object has a certain property or not.

We can check if an object has a particular property in php with the php property_exists() function.

Below are some examples of using property_exists() in php to check if a property exists in a php object.

class exampleClass {
    public $id;
    private $name;
    static protected $multiplyNumbers;

    static function multiplyNumbers($a,$b) {
        return $a * $b;
    }
}

var_dump(property_exists('exampleClass', 'name'));  
var_dump(property_exists(new exampleClass, 'name')); 
var_dump(property_exists('exampleClass', 'multiplyNumbers'));  
var_dump(property_exists('exampleClass', 'other'));   

// Output:
bool(true)
bool(true)
bool(true)
bool(false)

Checking if a Property Exists in php Object with Exception Handling

Another way you can check if a property exists in a php object is with exception handling.

When you try to access a property and the property doesn’t exist, then you will get an error. If you don’t get an error, you know the property is there.

Therefore, using this logic, we can check if a property exists in an object.

Below is an example in php of checking if different properties exist using exception handling.

class exampleClass {
    public $id;
    private $name;
    static protected $multiplyNumbers;

    static function multiplyNumbers($a,$b) {
        return $a * $b;
    }
}

try {
    exampleClass::doSomething();
}
catch(Error | Exception $e) {
    echo "\n Exception Caught: ", $e->getMessage();
}
    
try {
    exampleClass::multiplyNumbers(2,3);
    echo "\n This works!";
}
catch(Error | Exception $e) {
    echo "\n Exception Caught: ", $e->getMessage();
}

//Output: 
Exception Caught: Call to undefined method exampleClass::doSomething()
This works!

Hopefully this article has been useful for you to learn how to check if an object has an property or not in php.

Other Articles You'll Also Like:

  • 1.  php Square Root with sqrt Function
  • 2.  php pi – Get Value of pi Using php pi() Function
  • 3.  How to Create an Empty Array in php
  • 4.  php acos – Find Arccosine and Inverse Cosine of Number
  • 5.  Get Word Count of String in php with str_word_count() Function
  • 6.  php ceil – Round Up and Find the Ceiling of a Number in php
  • 7.  Add Months to Date in php
  • 8.  Capitalize First Letter of String with php ucfirst() Function
  • 9.  php Rename File – Renaming Files and Directories Easily
  • 10.  php array_diff() – Find Elements of Array Not Present in Other Arrays

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy