Php get method by string

Call method by string?

Examples to Implement PHP Call Function Let’s take examples to understand the concept of calling a function in PHP. Question: How can I call following Class method or function?

Call method by string?

Class MyClass< private $data=array('action'=>'insert'); public function insert() < echo 'called insert'; >public function run()< $this->$this->data['action'](); > > 

only possibilites is to use call_user_func(); ?

You can do it safely by checking if it is callable first:

$action = $this->data['action']; if(is_callable(array($this, $action)))< $this->$action(); >else< $this->default(); //or some kind of error message > 

Reemphasizing what the OP mentioned, call_user_func() and call_user_func_array() are also good options. In particular, call_user_func_array() does a better job at passing parameters when the list of parameters might be different for each function.

call_user_func_array( array($this, $this->data['action']), $params ); 

PHP | String Functions, strlen () function in PHP. This function takes a string as argument and returns and integer value representing the length of string. It calculates the …

PHP call Class method / function

How can I call following Class method or function?

Let say I have this params get from url:

To answer your question, the current method would be to create the object then call the method:

$functions = new Functions(); $var = $functions->filter($_GET['params']); 

Another way would be to make the method static since the class has no private data to rely on:

public static function filter($data) 

This can then be called like so:

$var = Functions::filter($_GET['params']); 

Lastly, you do not need a class and can just have a file of functions which you include. So you remove the class Functions and the public in the method. This can then be called like you tried:

Within the class you can call function by using :

you have to create an object of a class

 ex: $obj = new Functions(); $obj->filter($param); 

for more about OOPs in php

this example:

class test < public function newTest()< $this->bigTest();// we don't need to create an object we can call simply using $this $this->smallTest(); > private function bigTest() < //Big Test Here >private function smallTest() < //Small Test Here >public function scoreTest() < //Scoring code here; >> $testObject = new test(); $testObject->newTest(); $testObject->scoreTest(); 

Create object for the class and call, if you want to call it from other pages.

$obj = new Functions(); $var = $obj->filter($_GET['params']); 

Or inside the same class instances [ methods ], try this.

$var = $this->filter($_GET['params']); 
$f = new Functions; $var = $f->filter($_GET['params']); 

Have a look at the PHP manual section on Object Oriented programming

PHP Callback Functions, A callback function (often referred to as just "callback") is a function which is passed as an argument into another function. Any existing function can be used …

PHP Function to return string

I am fairly new to PHP. I have a function which checks the cost of price. I want to return the variable from this function to be used globally:

 else < $deliveryPrice="20"; >return $deliveryPrice; > // Assuming these two next lines are on external pages.. getDeliveryPrice(12); echo $deliveryPrice; // It should return 20 ?> 

You should simply store the return value in a variable:

$deliveryPrice = getDeliveryPrice(12); echo $deliveryPrice; // will print 20 

The $deliveryPrice variable above is a different variable than the $deliveryPrice inside the function. The latter is not visible outside the function because of variable scope.

 else < $deliveryPrice="20"; >return $deliveryPrice; > $price = getDeliveryPrice(12); echo $price; ?> 
 else < $deliveryPrice="20"; >//return $deliveryPrice; > // Assuming these two next lines are on external pages.. getDeliveryPrice(12); echo $deliveryPrice; // It should return 20 ?> 

As some alrady said, try using classes for this.

class myClass < private $delivery_price; public function setDeliveryPrice($qew = 0) < if ($qew == "1") < $this->delivery_price = "60"; > else < $this->delivery_price = "20"; > > public function getDeliveryPrice() < return $this->delivery_price; > > 

Now, to use it, just initialize the class and do what you need:

$myClass = new myClass(); $myClass->setDeliveryPrice(1); echo $myClass->getDeliveryPrice(); 

PHP GET Method | How PHP GET Method Works?, Below are the methods of PHP GET Method: Example #1 This is the example of using the GET METHOD of the PHP Programming Language. Here at first …

PHP Call Function

Introduction to PHP Call Function

In PHP, the functions are of two types i.e. built-in and user-defined functions. There are a lot of built-in functions that can be called directly from the program by the programmers or the developers. These built-in functions have a specific meaning for the task to be performed. A user-defined function is specifically developed by the program that contains the code to be performed with respect to the application. The developer writes a set of code that has to perform the task based on the requirement in the application.

Define and Call a Function in PHP

There are two types of Functions in PHP i.e. Built-in and user-defined functions. These functions are used in the program to perform various tasks. In PHP, a huge number of functions are defined and can be used anywhere in the program. Though it has a lot of advantages of not writing the code again and again and the functions come with the package itself, it is easy for the developer to use the built-in functions. But all the built-in functions have a predefined meaning and perform a specific task itself. So the developer started developing user-Defined Functions where the developer can write the specific task code inside any function and can be used anywhere in the program. The user-defined functions are called in the program in a class or in a method of a program to perform the task based on applications requirement.

How to Create a User-Defined Function?

In PHP, a User-defined function has to be declared with the keyword “function”. If we declare the function name and the code to be executed has been written inside it then it can be called anywhere in the program.

In the above syntax, the function_name is the name of the function that is to be called in the program and function is the keyword that is used to declare the function. In PHP, a function is declared with the function keyword prefixed with the function name and the calling of a function in a program is done by just calling the name of the function wherever required.

Examples to Implement PHP Call Function

Let’s take examples to understand the concept of calling a function in PHP.

Example #1

In the below example, the function Write_Output is the name of the function and the function is called in the program with the same name below. This line will call the defined function and executes the statements written inside the function and will exit once the last statement of the code gets executed.

PHP Call Function eg1

Example #2

1. In PHP, a function can be a parameterized one i.e. passing arguments to the function. An arguments are just like the variables we define in the program. We can simply pass the arguments after the name of the function inside the parenthesis and can be added as much as we want by adding a comma in between the arguments. These arguments will be mapped while calling the function. It will throw error if you are not passing the correct number of arguments when calling the function.

 Employee("Akash"); Employee("Prakash"); ?>

PHP Call Function eg2

In the above example, Employee is the name of the function and ename is the argument passed to the function in the declaration. The Employee function is called below the function declaration and the values are passed to the function while calling. The output of the program will be the names of the employee passed in the function while calling it.

2. In the below example, the employee names are printed and also the employee ID is printed along with it. When an organization has chunks of data to be stored in the database, we can simply write this kind of code to handle the data with ease.

 Employee("Lakshmi","780540"); Employee("Rohit","780541"); Employee("Jenny","780542"); ?>

Output

In PHP, the variables are not strictly based on the datatypes depending upon its value or data. The datatypes are loosely coupled and do not follow any strict rules. So we can add, subtract and do multiple operations on variables that have different data types as well.

Example #3

As we have seen all the examples, we can clearly see that the User-defined functions are more beneficial to the developer as it helps a lot from an application perspective and also is used to get the desired output. The goal of using functions in a particular program is that it can create his own code and develop the application based on requirements.

As we have seen in the above example that the integer can be multiplied with a string and a string can be used along with float etc. So PHP is a loosely typed language. In the above example, we could clearly see that the variable x and y are declared as integer and while calling they just use one variable as integer and another one as a string and the desired output also gets fetched.

Conclusion

In this article, we discussed how to call functions in PHP i.e. user-defined function. Then we discussed different forms of User-defined Functions and syntax with example. The functions can be written for a specific application and to perform a specific task i.e. calculation of payroll, adding new entries, etc. So the developer can easily modify the code for the flexibility and can call it anywhere in the program.

Final thoughts

This is a guide to PHP Call Function. Here we discuss how to define and how to create user-defined PHP Call Function along with examples and code implementation. You can also go through our other suggested articles to learn more –

Call php function from JavaScript, In this very basic example, we send a request to myAjax.php when the user clicks a link. The server will generate some content, in this case "hello world!". We will …

Источник

Get PHP class property by string

When working with PHP classes, there may be instances where you need to access a class property using a string. This can be useful in cases where the property name is not known until runtime or when you need to dynamically access properties.

Using the Magic Method

One way to access a class property by string is by using the magic method `__get()`. This method is automatically called when an undefined property is accessed. You can use this method to check if the property exists and return its value.

class MyClass < private $name = 'John Doe'; public function __get($property) < if(property_exists($this, $property)) < return $this->$property; > > > $obj = new MyClass(); echo $obj->name; // Output: John Doe 

In the example above, we defined a class `MyClass` with a private property `$name`. We then defined the magic method `__get()` which checks if the property exists using the `property_exists()` function and returns its value.

Using Reflection

Another way to access a class property by string is by using reflection. Reflection is a powerful feature in PHP that allows you to inspect and manipulate classes, properties, and methods at runtime.

class MyClass < private $name = 'John Doe'; >$obj = new MyClass(); $ref = new ReflectionClass($obj); $prop = $ref->getProperty('name'); $prop->setAccessible(true); echo $prop->getValue($obj); // Output: John Doe 

In the example above, we defined a class `MyClass` with a private property `$name`. We then created an instance of the class and used reflection to get the property `$name`. We set the property to be accessible using the `setAccessible()` method and then retrieved its value using the `getValue()` method.

Conclusion

In this guide, we have explored two different ways to access a class property by string in PHP. Using the magic method `__get()` and reflection are both great options depending on your use case. By using these techniques, you can dynamically access class properties and build more flexible and dynamic applications.

Источник

Читайте также:  What is javascript event listener
Оцените статью