php function inside function

Function inside a function.?

This code produces the result as 56.

Any idea what is going inside? I am confused.

8 Answers 8

X returns (value +3), while Y returns (value*2)

Although functions are not limited in scope (which means that you can safely ‘nest’ function definitions), this particular example is prone to errors:

The solution to both would be to split the code, so that both functions are declared independent of each other:

This is also a lot more readable.

Note that PHP doesn’t really support «nested functions», as in defined only in the scope of the parent function. All functions are defined globally. See the docs.

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

Not sure what the author of that code wanted to achieve. Definining a function inside another function does NOT mean that the inner function is only visible inside the outer function. After calling x() the first time, the y() function will be in global scope as well.

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

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

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

It is possible to define a function from inside another function. the inner function does not exist until the outer function gets executed.

Simple thing you can not call function y before executed x

Your query is doing 7 * 8

x(4) = 4+3 = 7 and y(4) = 4*2 = 8

what happens is when function x is called it creates function y, it does not run it.

function inside a function or so called nested functions are very usable if you need to do some recursion processes such as looping true multiple layer of array or a file tree without multiple loops or sometimes i use it to avoid creating classes for small jobs which require dividing and isolating functionality among multiple functions. but before you go for nested functions you have to understand that

so is this mean you cant use nested functions? No, you can with the below workarounds

first method is to block the child function being re declaring into global function stack by using conditional block with function exists, this will prevent the function being declared multiple times into global function stack.

and the second method will be limiting the function scope of child to local instead of global, to do that you have to define the function as a Anonymous function and assign it to a local variable, then the function will only be available in local scope and will re declared and invokes every time you call the main function.

remember the child will not be available outside the main function or global function stack

Источник

Consent to the use of Personal Data and Cookies

This website needs your consent to use cookies in order to customize ads and content.

If you give us your consent, data may be shared with Google.

Nested Functions in PHP

About using nested functions in PHP (functions inside of functions).

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

Edited: 2021-02-05 22:40

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

Nested functions (Aka: functions inside functions) are possible in PHP, and sometimes used in the form of anonymous functions.

It is also possible to create named functions inside of other functions, just as you do in procedural PHP; but I would not recommend this. One place where this might be useful is for polyfills, though I have not seen practical examples of it.

The problem with nested functions is that they are re-declared every time a function is called; which means that they come at a small performance hit. They also pollute the global scope, since they become available globally once declared; because of this, you may instead want to learn about Object Orientated PHP.

A nested function in procedural PHP looks like this:

Nested functions in PHP

When a function is defined inside a parent function, you will first have to call the parent function before the child function becomes available. Once the parent has been called, child functions will be available globally in your PHP script, and not just in the parent.

If you still want to get your hands dirty with nested functions, you should check out the below example:

You may also use the nested functions inside of the parent:

Anonymous functions

When you use an anonymous function inside a function or method it is also a type of nested function. The syntax of anonymous functions in PHP is similar to the syntax used in JavaScript.

Anonymous functions are useful for many purposes, one is when you use the array_walk function to «walk over» an array:

Object-oriented PHP

Using an objected orientated approach may be more approiate, and will also give more control over where the methods (functions) are accessible from.

For example, when defining functions (methods) inside of classes in PHP, it is possible to define them using the private and public keywords.

Источник

PHP function inside a class function include

I am working with lemonade-php. My code is at https://github.com/sofadesign/limonade.

The issue I am having is when I try to run

which then loads the fullwidth.tpl and runs function fullwidth

However when I try to run print_r($this->post($post)) inside the fullwidth function it says it can not find the post() function

I have tried a number of things like below

I tried re connecting to the syscore by

Here is my full class syscore

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

1 Answer 1

It looks like you are having some issues understanding the execution path that PHP is taking when trying to render your template. Let’s take a more in-depth look, shall we?

The trick to solving this one is to understand how PHP include works. When you call include(«./templates/<$this->temp>/fullwidth.tpl») some of your code is executing in the scope of the syscore object, namely:

Ok, so how do we solve it? Glad you asked.

We could probably refactor syscore::post to be a static method, but that would then require syscore::db to be static, and always return the SAME mongodb instance (singleton pattern). You definitely do not want to be creating 1000 Mongo instances for each syscore instance.

We could just abuse the framework. A much poorer solution, but it will get the job done.

Look the second way is a complete hack, and writing code like that will probably mean you won’t get promoted. But it should work.

But let’s say you wanted to get promoted, you would make good, shiny code.

Then in your fullwidth function, we don’t have to do any of that stupid nonsense of treating an instance method like it were a static method.

Источник

What are PHP nested functions for?

In JavaScript nested functions are very useful: closures, private methods and what have you..

What are nested PHP functions for? Does anyone use them and what for?

Here’s a small investigation I did

13 Answers 13

If you are using PHP 5.3 you can get more JavaScript-like behaviour with an anonymous function:

There is none basically. I’ve always treated this as a side effect of the parser.

Eran Galperin is mistaken in thinking that these functions are somehow private. They are simply undeclared until outer() is run. They are also not privately scoped; they do pollute the global scope, albeit delayed. And as a callback, the outer callback could still only be called once. I still don’t see how it’s helpful to apply it on an array, which very likely calls the alias more than once.

The only ‘real world’ example I could dig up is this, which can only run once, and could be rewritten cleaner, IMO.

The only use I can think of, is for modules to call a [name]_include method, which sets several nested methods in the global space, combined with

PHP’s OOP would obviously be a better choice 🙂

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

[Rewritten according to the comment by @PierredeLESPINAY.]

It’s not just a side-effect at all, but actually a very useful feature for dynamically modifying the logic of your program. It’s from the procedural PHP days, but can come in handy with OO architectures too, if you want to provide alternative implementations for certain standalone functions in the most straightforward way possible. (While OO is the better choice most of the time, it’s an option, not a mandate, and some simple tasks don’t need the extra cruft.)

For example, if you dynamically/conditionally load plugins from your framework, and want to make the life of the plugin authors super easy, you can provide default implementations for some critical functions the plugin didn’t override:

Functions defined within functions I can’t see much use for but conditionally defined functions I can. For example:

And then all your code needs to do is use the ‘cmp’ function in things like usort() calls so you don’t litter language checks all over your code. Now I haven’t done this but I can see arguments for doing it.

All the above being said, one might simply create a nested function to replace some localized, repetitive code within a function (that will only be used inside the parent function). An anonymous function is a perfect example of this.

Some might say just create private methods (or smaller code blocks) in a class, but that is muddying the waters when an ultra-specific task (which is exclusive to the parent) needs to be modularized, but not necessarily available to the rest of a class. The good news is if it turns out that you do need that function somewhere else, the fix is rather elementary (move the definition to a more central location).

Generally speaking, using JavaScript as the standard by which to evaluate other C based programming languages is a bad idea. JavaScript is definitely its own animal when compared to PHP, Python, Perl, C, C++, and Java. Of course, there are lots of general similarities, but the nitty, gritty details (reference JavaScript: The Definitive Guide, 6th Edition, Chapters 1-12), when paid attention to, make core JavaScript unique, beautiful, different, simple, and complex all at the same time. That’s my two cents.

Just to be clear, I’m not saying nested functions are private. Just that nesting can help avoid clutter when something trivial needs to be modularized (and is only needed by the parent function).

Источник

`static` keyword inside function?

I was looking at the source for Drupal 7, and I found some things I hadn’t seen before. I did some initial looking in the php manual, but it didn’t explain these examples.

What does the keyword static do to a variable inside a function?

7 Answers 7

You could use this for different purposes, for example:

In this example, the if would only be executed once. Even if multiple calls to doStuff would occur.

Seems like nobody mentioned so far, that static variables inside different instances of the same class remain their state. So be careful when writing OOP code.

If you want a static variable to remember its state only for current class instance, you’d better stick to a class property, like this:

Given the following example:

Therefore, the following call of

A static variable exists only in a local function scope, but it does not lose its value when program execution leaves this scope.

If you extend a class with static variables, the individual extended classes will hold their «own» referenced static that’s shared between instances.

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

static variable in a function means that no matter how many times you call the function, there’s only 1 variable.

Inside a function, static means that the variable will retain its value each time the function is called during the life of the page load.

Источник

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

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