php call function from function

How to call a function from a string stored in a variable?

I need to be able to call a function, but the function name is stored in a variable, is this possible? e.g:

17 Answers 17

php call function from function. Смотреть фото php call function from function. Смотреть картинку php call function from function. Картинка про php call function from function. Фото php call function from function

My favorite version is the inline version:

You can place variables or expressions inside the brackets too!

Solution: Use PHP7

Note: For a summarized version, see TL;DR at the end of the answer.

Old Methods

Update: One of the old methods explained here has been removed. Refer to other answers for explanation on other methods, it is not covered here. By the way, if this answer doesn’t help you, you should return upgrading your stuff. PHP 5.6 support has ended in January 2019 (now even PHP 7.1 and 7.2 are not being supported). See supported versions for more information.

As others mentioned, in PHP5 (and also in newer versions like PHP7) we could use variables as function names, use call_user_func() and call_user_func_array() (which, personally, I hate those functions), etc.

New Methods

As of PHP7, there are new ways introduced:

Note: Everything inside brackets means one or more expressions to form something, e.g. means expressions forming a function name.

Dynamic Function Call: Function Name On-the-fly

We can use one or more expressions inside parentheses as the function name in just one go, in the form of:

Dynamic Method Call: Method Name On-the-fly

Just like dynamic function calls, we can do the same way with method calls, surrounded by curly braces instead of parentheses (for extra forms, navigate to TL;DR section):

See it in an example:

Dynamic Method Call: The Array Syntax

A more elegant way added in PHP7 is the following:

Note: The benefit of using this method over the previous one is that, you don’t care about the call type (i.e. whether it’s static or not).

Note: If you care about performance (and micro-optimizations), don’t use this method. As I tested, this method is really slower than other methods (more than 10 times).

Extra Example: Using Anonymous Classes

Making things a bit complicated, you could use a combination of anonymous classes and the features above:

TL;DR (Conclusion)

Generally, in PHP7, using the following forms are all possible:

php call function from function. Смотреть фото php call function from function. Смотреть картинку php call function from function. Картинка про php call function from function. Фото php call function from function

Yes, it is possible:

If the function you are calling belongs to an object you can still use either of these

A few years late, but this is the best manner now imho:

php call function from function. Смотреть фото php call function from function. Смотреть картинку php call function from function. Картинка про php call function from function. Фото php call function from function

My specific case is when implementing someone’s code having to do with colors and two member methods lighten() and darken() from the csscolor.php class. For whatever reason, I wanted to have the same code be able to call lighten or darken rather than select it out with logic. This may be the result of my stubbornness to not just use if-else or to change the code calling this method.

This above attempt would give a warning about the method expecting a 2nd argument (percent).

Источник

How to auto call function in php for every other function call

Now my question is,

How can i call another function before any called function execution? In above case, how can i auto call ‘test1’ function for every another function call, so that i can get the output as,

currently i am getting output as

I cannot call ‘test1’ function in every function definition as there may be many functions. I need a way to auto call a function before calling any function of a class.

Any alternative way would also be do.

8 Answers 8

Your best bet is the magic method __call, see below for example:

Updated: Add link to ideone, add example with return value.

Here’s the simple example, taken from my slides:

And here’s our «poor» interceptor logic:

Now if we have the following method to be called:

Then this won’t work:

This applies to all solutions that suggest implementing a «proxy».

Solutions suggesting explicit usage of other methods that then call your internal API are flawed, because they force you to change your public API to change an internal behavior, and they reduce type safety.

Here is a solution that does solve this problem partially:

Here is how you use it:

This is a type-safe, clean solution, but it involves a lot of coding, so please take it only as an example.

Writing this boilerplate code is NOT fun, so you may want to use different approaches.

To give you an idea of how complicated this category of problems is, I can just tell you that I wrote an entire library to solve them, and some smarter, wiser, older people even went and invented an entirely different paradigm, called «Aspect Oriented Programming» (AOP).

Therefore I suggest you to look into these 3 solutions that I think may be able to solve your problem in a much cleaner way:

then just use build_wrapper as you like.

Use the PHP AOP Extension, which I don’t believe to be a good solution, mainly because GO-AOP-PHP solves this problem in a more elegant/debuggable way, and because extensions in PHP are inherently a mess (that is to be attributed to PHP internals, not to the extension developers). Additionally, by using an extension, you are making your application as un-portable as possible (try convincing a sysadmin to install a compiled version of PHP, if you dare), and you can’t use your app on cool new engines such as HHVM.

Источник

php: determine where function was called from

is there a way to find out, where a function in PHP was called from? example:

8 Answers 8

Example:

Output:

Fastest and simplest solution as I found

I test the speed on Lenovo laptop: Intel Pentiom CPU N3530 2.16GHz, RAM 8GB

php call function from function. Смотреть фото php call function from function. Смотреть картинку php call function from function. Картинка про php call function from function. Фото php call function from function

So if you still REALLY don’t know how, than here is solution:

php call function from function. Смотреть фото php call function from function. Смотреть картинку php call function from function. Картинка про php call function from function. Фото php call function from function

If you want to trace the exact origin of the call at the top of the stack you can use the following code:

This will ignore chained functions and get only the most relevant call info (relevant is used loosely as it depends what your are trying to accomplish).

Hope it helps somebody. If the actual function is outside httpdocs then it can not be found because the server will be setup to not allow it. Only tested it one folder deep too but the recursive methodology should work in theory.

This is like version 0.1 but I don’t intend on continuing development on it so if someone updates it feel free to repost it.

Not the answer you’re looking for? Browse other questions tagged php function-calls or ask your own question.

Linked

Related

Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

site design / logo © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. rev 2021.9.17.40238

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Источник

How to call function of one php file from another php file and pass parameters to it?

I want to call a function in one PHP file from a second PHP file and also pass two parameters to that function. How can I do this?

I am very new to PHP. So please tell me, should I include the first PHP file into the second?

Please show me an example. You can provide some links if you want.

4 Answers 4

Yes require the first file into the second. That’s all.

See an example below,

Now Using require (http://php.net/require) to require the File1.php to make its content available for use in the second file:

php call function from function. Смотреть фото php call function from function. Смотреть картинку php call function from function. Картинка про php call function from function. Фото php call function from function

sum of two numbers 6 product of two numbers 6

Note: don’t write public before function. Public, private, these modifiers can only use when you create class.

php call function from function. Смотреть фото php call function from function. Смотреть картинку php call function from function. Картинка про php call function from function. Фото php call function from function

php call function from function. Смотреть фото php call function from function. Смотреть картинку php call function from function. Картинка про php call function from function. Фото php call function from function

you can write the function in a separate file (say common-functions.php) and include it wherever needed.

You can include common-functions.php in another file as below.

You can include any number of files to another file. But including comes with a little performance cost. Therefore include only the files which are really required.

Not the answer you’re looking for? Browse other questions tagged php or ask your own question.

Linked

Related

Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

site design / logo © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. rev 2021.9.17.40238

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Источник

Calling a function within a Class method?

I have been trying to figure out how to go about doing this but I am not quite sure how.

Here is an example of what I am trying to do:

Here is the part I am having problems with, how do I call bigTest()?

10 Answers 10

The sample you provided is not valid PHP and has a few issues:

The syntax should rather be:

Second, wrapping the bigTest() and smallTest() functions in public function() <> does not make them private — you should use the private keyword on both of these individually:

Also, it is convention to capitalize class names in class declarations (‘Test’).

php call function from function. Смотреть фото php call function from function. Смотреть картинку php call function from function. Картинка про php call function from function. Фото php call function from function

I think you are searching for something like this one.

php call function from function. Смотреть фото php call function from function. Смотреть картинку php call function from function. Картинка про php call function from function. Фото php call function from function

php call function from function. Смотреть фото php call function from function. Смотреть картинку php call function from function. Картинка про php call function from function. Фото php call function from function

php call function from function. Смотреть фото php call function from function. Смотреть картинку php call function from function. Картинка про php call function from function. Фото php call function from function

In order to have a «function within a function», if I understand what you’re asking, you need PHP 5.3, where you can take advantage of the new Closure feature.

You need to call newTest to make the functions declared inside that method “visible” (see Functions within functions). But that are then just normal functions and no methods.

Источник

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *