The question is about functions and arguments in PHP. I am reading code of simple extension for mediawiki.
It adds callback function:
$wgHooks['ArticleSaveComplete'][] = 'fnAutoWikiDump';
and then there is definition of this function:
function fnAutoWikiDump(&$article, &$user, &$text, &$summary, &$minoredit,
&$watchthis, &$sectionanchor, &$flags, &$revision){...}
inside this function I can access members of class $article:
$awd_title = $article->getTitle();
I cannot understand how variable $article was passed to the function while calling it? It looks like it was passed in definition of the function (I know it is wrong), but I don't understand how it was passed.
Do you?
The code you have posted, and the sample code in the link do not show the details of actually calling the function. It is merely registered as a callback. Part of the usefulness of callback functions is that you typically do not have to call them yourself at all, but rather they are called automatically by the process that uses them.
Somewhere else in the MediaWiki code, where the callbacks registered with ArticleSaveComplete are called (there's an array of them), the correct parameters are passed to the function call in a regular and recognizable function call or via a mechanism like call_user_func().
When defining a callback to work with that interface, your responsibility as a programmer is only to be sure that the function definition takes the correct number of parameters in the correct order, and with the correct names. The details of how it gets called are up to the mechanism that calls it.
Related
I wondered what is the difference between
$x, $y, $z being variables declared and the function($x,$y,$z)
and
$_SESSION['x'], $_SESSION['y'],$_SESSION['z'] declared
and
function() using $_SESSION['x'], $_SESSION['y'], $_SESSION['z'];
Best regards, thanks.
In the first scenario, the values are provided to the function by whatever invokes that function. In the second, the function actively seeks out the variables from the session. The distinction between the two comes down to where the responsibility for finding the values should happen.
The session is a dependency. So if the function relies on the session, then the function has an internal dependency. This limits the versatility of the function.
If the function accepts the values as arguments, any PHP code anywhere can use the code in that function. If the function pulls the values from session state, then only an active web application can use that function. Plus that web application has to remember to put the values where the function can find them (since it doesn't advertise that it needs those values, as function arguments do).
In the majority of cases, the first approach is preferred. If the function performs an operation (and optionally returns a result), then have the function just do that. Have it "do one thing". The responsibility of providing the function with what it needs belongs to the code which calls the function. The function itself, as a fully-encapsulated entity, should simply advertise what it needs and what it is expected to return.
I have a function
function func(){ //somecode }
and function call_func(){ func(); }
so how much memory does call_function use?
and if I used many function like call_func_1() call_func_2() call func_3()
to call func() is it bad?
I got this question because this
I am using codeigniter framework,
In codeigniter if I have a controller php file named location.. and in that location controller, there is a function named index(),
Inside Index() function I have a code that receive get parameter that is used for searching something and output it as page..
so if i access www.something.com/location?city="Indonesia"&location="jakarta"
It will display the search result...
I wanted to make the link pretty by calling www.something.com/location/location_in_indonesia and the parameter I use Post
So in codeigniter if i used www.something.com/location/location_in_indonesia I will be calling a function named location_in_indonesia from location.php because I dont want to copy the code that do the search, I just use
function location_in_indonesia(){
$this->index()
//which called the function index and display the search result
}
and if i have another country to search then i need to add many function
But later after I posted this,I think this can be resolved by using routes feature that is provided by codeigniter. I was wondering about if it is bad to use that function that is just calling another function. So I asked this question.
When you call a function from within another function this is what happens (note this is a generic description and not really PHP specific).
The calling function will push its current state into the call stack which as the name implies, is a stack (meaning the status will be placed on the top). The information that gets pushed is (roughly speaking) the position within the function at which the function call happened and references to variables in scope.
Now this implies that the memory taken in the stack will basically be some constant + the number of variables in scope. In a programming language like C, the size that this stack can take up is pretty much constant and allocated by the OS, in Java you can specify this size via JVM parameters but more or less this means that there's a finite number of functions within functions that one can call (this is one of the reasons why there's so much research being done to convert recursive algorithms to iterative ones).
Now what this means for your case is that the extra memory required for the function call is just a pointer to where the calling function was when the call occurred, which is basically a very low price to pay.
Overall what you are doing is pretty common and I have no indication to believe that it's bad practice (although routing is probably better practice for your specific case).
Also, if you're using a PHP framework, then the extra cost of a single function call is very small compared to everything else that the framework is doing.
Here is an example.
$vars['jscss_src'] = "\n".implode("\n",$aSrc)."\n";
$this->vars($vars);
There is an expression $this->load->vars() but when googling $this->vars()
Nothing could be found. What is the meaning of it?
Thanks in advance :)
+) actually this expression is coded in the extended Loader core class in codeigniter. Could it be a reason?
This code almost certain is part of a member function of some class. The this is referencing the actual object, the method has been called on. vars is another member function of the same class.
According to the documentation:
$this->load->vars($array)
This function takes an associative array as input and generates
variables using the PHP extract function. This function produces the
same result as using the second parameter of the $this->load->view()
function above. The reason you might want to use this function
independently is if you would like to set some global variables in the
constructor of your controller and have them become available in any
view file loaded from any function. You can have multiple calls to
this function. The data get cached and merged into one array for
conversion to variables.
The load->vars function adds the array to an Loader class variable that is used in the load->view function where the array gets extracted. (Is used to pass variables to views)
I am studying PHP and i am trying to understand about callback functions, I really looked around at the manual, At stackoverflow and more websites, And i really dont understand what is PHP CallBack Function , If can some one can please help me understand about this functions, I am looking for simple explanation/guide thank you all and have a nice day.
Take a look at Wikipedia - Callback
In computer programming, a callback is a reference to a piece of executable code that is passed as an argument to other code. This allows a lower-level software layer to call a subroutine (or function) defined in a higher-level layer.
It's a function what you pass to your method or some other function, so it can be called later during that method - function execution.
For example you have callback beforeSave and you want do some logic before saving data to database file etc.. (in one place - DRY). You add logic into beforeSave callback and this callback being called before data saved.
Same is with function on manual for example array_filter($input, callback) it requires that you pass some function to be executed with $input data.
For ex. pass anonymous function:
array_filter($input, function($var) {
// returns whether the input integer is odd
return($var & 1)
});
Will return to you all odd array values, you can change logic in anonymous function to what you want, but array_filter internal mechanic will be always the same (iterator algo)
In the script below, does the order in which items are declared matter?
For example, if the add_action points to a function that has not yet been defined? Does it matter or should the function declaration always precede any code in which its called?
add_action('load-categories.php', 'my_admin_init');
function my_admin_init(){
//do something
}
That doesn't matter if the function is declared before or after the call but the function should be there in the script and should be loaded in.
This is the first method and it will work:
some_func($a,$b);
function some_func($a,$b)
{
echo 'Called';
}
This is the second method and will also work:
function some_func($a,$b)
{
echo 'Called';
}
some_func($a,$b);
From the PHP manual:
Functions need not be defined before they are referenced, except when a function is conditionally defined as shown in the two examples below.
However, while this is more of a personal preference, I would highly recommend including all the functions you actually use in an external functions.php file then using a require_once() or include_once() (depending on tastes) at the very top of your main PHP file. This makes more logical sense -- if someone else is reading your code, it is blindingly obvious that you are using custom functions and they are located in functions.php. Saves a lot of guesswork IMO.
you can call a function before it's defined, the file is first parsed and then executed.
No.
It is not C :P...
As you can see here , the whole file is first being parsed and then executed.
If a function that doesn't exist is being called, php will throw an error.
Fatal error: Call to undefined function
As per my personal experience, In some special cases (Like, passing array's in function or function inside a function and so on). It's best option to define the function above the call. Because of this sometimes neither function works nor PHP throw an error.
In normal php functions, it doesn't matter. You can use both of the types.
It does not matter, as long as it is declared somewhere on the page.
as seen here:
http://codepad.org/aYbO7TYh
Quoting the User-defined functions section of the manual :
Functions need not be defined before
they are referenced, except when a
function is conditionally defined
So, basically : you can call a function before its definition is written -- but, of course, PHP must be able to see that definition, when try to call it.