How To Call A method (function) From the Class in PHP Oops

You know that in object-oriented programming, we write all our code statements in a class. So here, you will know that if a class has a function in it then how to call it.

  • The first step is to create an object by class name. And make sure you added a ‘new‘ keyword before. Store this object in a variable like that:- $result = new test();
  • Now, $result is an object, So to call function access it from $result object as a method like this : – $result->first(); See the example below.
<?php

class test{
	function first()
	{
		echo "Function is called.";
	}
}

$result = new test();
$result->first();

?>

Note *** In object-oriented programming, variables inside a class called as property and functions are known as methods.


Also, read these articles

  1. how to split date & time in PHP
  2. Print timezone list
  3. date calculation
  4. Ch-1: beginner guide to SQL language
  5. How to create a database using SQL?
  6. How to delete a database?
  7. Why you are not able to delete the database?
  8. How to create a table in SQL?
  9. How to delete a table?
  10. How to insert data in a table?

Leave a Comment