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)
Related
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.
Is it possible to retrieve all the functions included in a global variable?
I'm using a very complex plugin (not written by me) that call the functions in this way:
$GLOBALS['myplugin']->member->isActive()
Is there a way to retreive all the functions of $GLOBALS['myplugin']->member?
You may find some success with get_class_methods(), passing in the name of the class that member is an instance of, or the object instance itself.
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.
I am confused about variables scope when calling a view from within a view.
I tested a bit and found:
If the variables are originally passed from a controller, there is no need to do something to pass the variables from a view to a view.
If the variables are declared in a view, I have to explicitly pass the variables from a view to a view. (e.g. : $this->load->view("hoge", $data);)
I find it bit odd about the second case because my understanding was $this->load->view() is codeigniter version of the php include() which doesn't require me to pass variables explicitly.
Can anyone guess/shed a little light on why they did this?
If you look at the Loader library's _ci_load method (which view() calls), around line 639 in the latest version, you'll see this bit of code:
/*
* Extract and cache variables
*
* You can either set variables using the dedicated $this->load_vars()
* function or via the second parameter of this function. We'll merge
* the two types and cache them so that views that are embedded within
* other views can have access to these variables.
*/
if (is_array($_ci_vars))
{
$this->_ci_cached_vars = array_merge($this->_ci_cached_vars, $_ci_vars);
}
extract($this->_ci_cached_vars);
That's why your variables passed to the view are available automatically in nested views.
But your locally declared variable aren't.. because they aren't passed on to the next view() call.
Note that it merges the variables, so technically, you could just pass to your subview the variable that are changed in the top view, the rest will be inherited automatically.
IMHO though, I think that for the sake of clarity and other potential people reading your code it's best to always pass on explicitly all the variables that your subview will require.. code becomes easier to read/debug.
Note: a side effect of the caching is that if you have 2 subviews, variables passed to the first one will get cached and get automatically passed on to the second view as well.. that can lead to debugging trouble sometimes.
Can someone please tell me what the best way to pass values from a controller into a view would be? If anyone has played with codeignitor, they will know what I mean. I have looked at CIs code but can't find the file that handles this. I'd LOVE to know how this is done.
Thanks!
There's not necessarily a "best" way as far as I know, but there is a common method that I've seen used many times, and have used myself. It generally involves an associative array, and either the extract() function or variable variables.
Basically, all you do is set up your data into an associative array, using keys that will become your template variables.
//inside the controller
$data['name'] = 'my name';
$data['zip'] = '90210';
The $data array gets passed to the view somehow, either directly or indirectly, and extracted via extract() or using a loop of variable variables (same thing, really). The template can then be included, and the variables are in local scope.
//inside the view rendering process
extract($data);
//$name and $zip now exist
Code Igniter follows this exact procedure. Inside system\libraries\Loader.php in the most recent version (1.7.1) there's a function called view(), which is what you call in your CI controller to load a view/template (same thing really in CI). You pass a data array as the second parameter.
view() calls an internal function called _ci_load() in the same file, which extracts the data you passed it (and does some other wacky caching stuff). Your variables are ready to go after that in the local function scope, and can be manipulated inside the template after the subsequent include(), since everything happening in the included file exists in the local _ci_load() function scope as well.
I've used the exact same design in a quick-and-dirty homebrew MVC set up before. It's quite effective.
You might want to try CakePHP's 15-min blog sample. I haven't tried Code Igniter.
In Zend Framework, it's as simple as
class IndexController {
public function IndexAction {
$this->view->name='Name';
}
}
with the $this->view->xxxx setting the variable in the view.