Values between functions

Values between functions

by Victor Martin -
Number of replies: 5

Hi, I'm trying to pass a variable value of a function to use in the function of my module as extended moodleform (definition), and as I'm a little green I do not know how I should do It. I come from the world of C, C + + where I pass this variable as an argument to the function from the main, but I think php is a different approach. I read some things about GLOBALS and types Hidden, but I don't have it very clear.

Can you clarify this doubt? Thank you very much.

Average of ratings: -
In reply to Victor Martin

Re: Values between functions

by Eric Millin -

Php is written in C, so the two languages work very similarly.  You can think of each page as its own "Main" function, with the globals serving as the initial arguments.  You can also think of globals as variables declared in a header file that all files have implicitly included.

The "include" and "require" statements that you see work just like the "include" declarations in C/C++.  Generally, including another script brings its contents into scope so that its functions/classes can be used.

Another difference is that Php can be written purely procedurely.  Unfortunately, much of Moodle's page logic is written this way.  Whenever you see that, imagine the page looks like this:

include('foo.php');

void main(object $GLOBALS[])
{

$_REQUEST = $GLOBALS['_REQUEST'];

//rest of the page contents...

}

That should look familiar.  Also, Php is loosely typed, so a variable's type needn't be declared and can change dynamically.  E.g.,

$foo = "foo";

echo $foo;  //prints "foo"

$foo = 5;

$bar = $foo + 5;

echo $bar; //prints 10

However, in the end, it's all C, so it shouldn't take you long to put all the pieces together.

Average of ratings: Useful (2)
In reply to Eric Millin

Re: Values between functions

by Hubert Chathi -

Just because PHP is written in C doesn't mean that the languages are similar.  For example, Haskell is written in C, but it is quite different from C.

The PHP language is C-like in many ways, and it certainly takes certain ideas from C, but there are very significant differences between the two.

In reply to Victor Martin

Re: Values between functions

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

To pass extra data when creating a Moodle form, you can use the $customdata argument on the constructor. That data is then available as $this->_customdata within the form class. If you need lots of data, pass in an array.