Open In App

How to find out where a function is defined using PHP ?

Last Updated : 01 Jun, 2021
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

When we do projects then it includes multiple modules where each module divided into multiple files and each file containing many lines of code. So when we declare a function somewhere in the files and forget that what the function was doing or want to change the code of that function but can’t find that where the function is! So this article will help you to find the location of the function.
In order to get the location of the function in PHP we can use the inbuilt class ReflectionFunction(). When the name of the function regarding which we are requiring details is passed to the class constructor, it gets several details regarding that function. 
 

  • getFileName: It returns the file location of the function.
  • getNumberOfParameters: It returns the number of parameters passed to the function.
  • getStartLine: It return the starting line of the function.

Syntax: 
 

$details = new ReflectionFunction('function_name');

Then use the above functions to access whatever you need. Paste the below code into main code and you will get the details of that function. 
 

PHP




<?php
$details = new ReflectionFunction('printing');
print $details->getFileName() . ':' . $details->getStartLine();
?>


Steps to run the code: 
 

  • Create a file named as file_name.php
  • Copy the above given code into the file.
  • Save the file into your localhost server folder. In the case of WampServer the look for the folder ‘www’ inside ‘wamp64’ folder in C drive and save the file there.
  • Run your wamp server.
  • Open any browser and type localhost/fun.php to get the following output.

Output: 
 

C:\wamp64\www\file_name.php : 2 

Below examples illustrates the ReflectionFunction in PHP:
Example 1: Suppose in the given code we want to find the location of the function ‘printing’. In the output, the filename and location of the function printing can be seen.
 

php




<?php
function printing()
{
    echo 'Welcome to GeeksforGeeks';
}
 
$details = new ReflectionFunction('printing');
echo 'File location : '.$details->getFileName().
', Starting line : ' . $details->getStartLine().
', Parameters passed : '.$details->getNumberOfParameters();
?>


Output: 
 

File location : /home/7de5f19b219d214c719df5f3839a7f61.php, 
Starting line : 2, Parameters passed : 0

Example 2: Suppose in the given code we want to find the location of the function ‘geeks’. In the output, the filename, location, start line and the parameter passed of the function geeks can be seen.
 

php




<?php
 
function printing() {
    echo 'Welcome to GeeksforGeeks';
}
 
function geeks() {
    echo 'This is the article How to find out where
         a function is defined using PHP?';
}
 
$details = new ReflectionFunction('geeks');
 
print 'File location :'.$details->getFileName().
' Starting line :' . $details->getStartLine().
' No. of parameters passed :'.$details->getNumberOfParameters();
 
?>


Output: 
 

File location :/home/dd96d70bdf5ff03fea0ea24110bae9ff.php 
Starting line :7 No. of parameters passed :0

 



Next Article

Similar Reads

How the User-Defined Function differs from Built-in Function in PHP ?
In PHP, functions are blocks of reusable code that perform specific tasks. They enhance code readability, modularity, and maintainability by encapsulating logic into named units. PHP functions can be categorized into two main types: user-defined functions and built-in functions. User-Defined FunctionsUser-defined functions are functions created by
2 min read
PHP | defined() function
The PHP defined() function is an inbuilt function in PHP which checks whether a constant is exists or not, in other words, defined or not. Syntax: bool defined($constant_name); Parameter: This function accepts a single parameter as mentioned above and described below. $constant_name: This is required parameter. It specifies the name of the constant
1 min read
What are User-defined Functions and Built-in Functions in PHP?
In PHP, User-defined functions are created by programmers to meet specific requirements, while PHP built-in functions are provided by PHP to perform common tasks without the need for manual implementation. Both types of functions play crucial roles in PHP development, offering flexibility, modularity, and efficiency in coding. Table of Content User
2 min read
How to check a function is defined in JavaScript ?
In this article, we will check whether a function is defined or not in JavaScript. The JavaScript typeof operator is used to check whether the function is defined or not. JavaScript typeof OperatorThe typeof operator is used to find the type of a JavaScript variable. This operator returns the type of a variable or an expression. Syntax: typeof varP
2 min read
How to find out the caller function in JavaScript?
In this article, we see the methods to find out the caller function in Javascript. Sometimes, the developer may want to modify how a function works on the basis of its caller function. To find out the caller function name, we will use the Function object's caller property. Property: Function.caller Here, the Function object is replaced by the name
1 min read
How to find out how many Times a Function is Called with JavaScript ?
In JavaScript, a function can be called multiple times wherever you need the same kind of functionality. You can also keep track of the function calls to find out how many times it is called in the whole code using the below approaches. Table of Content Using a Counter VariableUsing a Wrapper FunctionUsing a Proxy ObjectUsing a Counter VariableIn t
2 min read
How to create a variable using a user-defined name in JavaScript ?
In this article, we will learn to create a variable using the user-defined name in JavaScript. Variables in JavaScript: Basically, it is a container that holds reusable data in JavaScript. The variable's value can be changed during program execution and should be declared before using it. Before ES6 declares a variable, we just use the var keyword
2 min read
How to print out all the credits for PHP?
Given a PHP document we need to print all the credits for PHP. This can be done in a number of ways like if we want to print all the credits at once we can do it by using the flag parameter CREDITS_ALL, if we only want to print the documentation credits we can do the same by using the flag parameter CREDITS_DOCS and so on. Approach 1:For printing t
4 min read
How to get random value out of an array in PHP?
There are two functions to get random value out of an array in PHP. The shuffle() and array_rand() function is used to get random value out of an array. Examples: Input : $arr = ("a"=>"21", "b"=>"31", "c"=>"7", "d"=>"20") // Get one random value Output :7 Input : $arr = ("a"=>"21", "b"=>"31", "c"=>"7", "d"=>"20") // Get two
3 min read
How to find out which Character Key is Pressed using JavaScript?
To find which key is pressed in JavaScript, use event listeners like keydown, keypress, or keyup. By accessing properties such as event.key or event.code, it becomes easy to identify the specific key pressed. This approach is useful for handling user input and triggering actions based on key events. ApproachAttach an event to the input box. like th
2 min read
JavaScript is showing reference error "Prompt is not defined"
In this article, we cover the reference error that is "Prompt is not defined". It's important to understand the tools you are using. I know, this can be very overwhelming in the beginning. but here, we will solve this reference error with not only one solution but also make more ways to done properly. window.prompt() instructs the browser to displa
2 min read
Why error "$ is not defined" occurred in jQuery ?
One of the most common errors faced by jQuery developers is the '$ is not defined' error. At first, it may seem like a small error, but considering the fact that more than 70 percent of the website uses jQuery in some form or other, this may turn out to create a huge mess. Reason behind this error: This error basically arises, when the developer is
2 min read
How to detect which one of the defined font was used in a web page?
So here, we will be discussing some methods which will help you to get your desired font from any website ! 1.WhatFont : Extensions such as WhatFont make it easy to detect the font on webpage. You just need to install it and click on the element you want to inspect. The result is shown in a floating box, in the context of your choice. It is availab
2 min read
JavaScript ReferenceError - variable is not defined
This JavaScript exception variable is not defined and occurs if there is a non-existent variable that is referenced somewhere. Message: ReferenceError: "x" is not defined Error Type: ReferenceError Cause of Error: There is a non-existent variable that is referenced somewhere in the script. That variable has to be declared or make sure the variable
1 min read
D3.js line.defined() method
The d3.line.defined() method lets you specify whether there is data defined for a given data point or not. If this method returns false, this means that data point exists, else true. Syntax: d3.line.defined(data_point); Parameters: data_point: data_point to be checked. Return Value: This method returns a boolean value. Example 1: In this example, w
2 min read
D3.js lineRadial.defined() method
The d3.lineRadial.defined() method lets you specify whether there is data defined for a given data point, angle, or not. If this method returns false, this means that data point exists, else true. Syntax: d3.lineRadial.defined(data_point); Parameters: data_point: data_point to be checked, it can be an angle, radius, etc. Return Value: This method r
2 min read
D3.js areaRadial.defined() Method
The areaRadial.defined() method in D3.js lets one specify whether data is defined for a given data point or not. The value is checked for each point using the boolean value or function specified as a parameter to this method. The checked value is kept if this method returns true, otherwise it is omitted. Syntax: areaRadial.defined( data_point ) Par
2 min read
D3.js area.defined() Method
The d3.area.defined() method lets you specify whether there is data defined for a given data point or not. If this method returns false, this means that data point exists, else true. Syntax: d3.area.defined(data_point) Parameters: This function accepts a single parameter as mentioned above and described below. data_point: This parameter is the data
2 min read
How to hide elements defined as variables in jQuery ?
In this article, we will learn how to hide elements defined as variables in jQuery. These can be done using two approaches. Approach 1: In this approach, we will first select the element that has to be hidden and then assign it to a variable. We will then call the hide() method on the variable. This method will hide the element from the page. Examp
2 min read
Explain the difference between undefined and not defined in JavaScript
In JavaScript, they both are related to memory space and there is a very simple difference between them. If the variable name which is being accessed doesn't exist in memory space then it would be not defined, and if exists in memory space but hasn't been assigned any value till now, then it would be undefined. undefined: It is a JavaScript keyword
3 min read
Uncaught ReferenceError: controller is not defined
Uncaught ReferenceError: the controller is not defined is a common error message that can occur in JavaScript. It usually occurs when you try to access a variable or function that has not been defined or is out of scope. An example where this error occurs: For example, the following code will result in an Uncaught ReferenceError: the controller is
1 min read
How to allow classes defined in a module that can be accessible outside of the module ?
The TypeScript scripts written by default are in the global scope which means that all the functions, methods, variables, etc. in one file are accessible in all other TypeScript files. This can lead to conflicts in variables, functions as programmers can edit the function/variable name or value without any realization. Therefore, the concept of mod
3 min read
Uncaught ReferenceError: Angular is not defined
In this article, we will see the "Uncaught ReferenceError: Angular is not defined", along with knowing a possible way to resolve it. The ReferenceError object depicts an error that arises when the variable is not initialized yet or simply, the variable doesn’t exist in the current scope & is referenced. This error might arise due to the angular
2 min read
JavaScript for...in loop not working - Object property is not defined Error
A for...in loop is a special loop in JavaScript that enumerates all the keys (object property names) of an object. With each new iteration, a new property string is assigned to the loop variable. Error: Uncaught ReferenceError: Object property is not defined If we try to compare this loop variable with a non-string variable a ReferenceError is gene
3 min read
How are actions defined in Redux ?
Redux is the state management library for JavaScript apps. It is used to manage the data globally in the app. In redux, there are three parts: Actions ReducerStoreActions: Actions are the JavaScript objects that have information. Actions provide information for the store. There is one more term that is used to create the action called Action creato
3 min read
How to Fix the "partial is not defined" Error in EJS ?
Encountering an error message like "partial is not defined" can be a stumbling block when you're working with EJS (Embedded JavaScript) templates. This error typically pops up when you're trying to use the `partial` function in EJS, but it's not being recognized. Don't worry, though it's a common hiccup for many developers, and we've got you covere
3 min read
How to Fix "ReferenceError: __dirname is Not Defined in ES Module Scope" in Javascript
The JavaScript developers working with the Node.js often encounter the error "ReferenceError: __dirname is not defined in the ES module scope" when transitioning from the CommonJS modules to the ES modules. This error occurs because the __dirname and __filename globals which are available in the CommonJS are not defined in the ES module scope. Unde
4 min read
How to specify a list of pre-defined options for input controls in HTML?
Specify a list of pre-defined options for input controls by using the HTML list Attribute. A pre-defined option for input controls using HTML5's <datalist> element, allowing users to select from a predefined list while typing in an input field. Syntax: <input list="datalist_id"/>Example 1: The example below shows, the HTML form with an
2 min read
How to Fix Error EJS 'partial is not defined'?
This article provides a detailed explanation of the ejs 'partial is not defined' error, detailing various approaches to resolve it, from using native EJS features to creating custom functions. The error 'partial is not defined' occurs in EJS when you use a partial function that hasn't been defined. EJS itself doesn't come with a built-in partial fu
3 min read
JavaScript Program to Check if String Follows Order of Characters Defined by a Pattern or Not
In this JavaScript article, we will see the solution to the problem of checking if a string follows the order of characters defined by a pattern or not. We have given a string and pattern as the input and our task is to develop a program that will check whether the input string follows the same pattern according to the pattern given. Example: To so
4 min read
  翻译: