In php when initializing a class how would one pass a variable to that class to be used in its functions? -
So this is a deal I want to call a class and want to give it a value so that it can be used for all classes Can be used in the Act. Ect.
Thanks, Sam
I want to call a class And I want to give it a value so that it can be used within that class.
This concept is called "Constructor".
As the other answer, you should use ( __ construct ()
) as PHP (5). This is an example of what it looks like:
class Foo {function __construct ($ Init_parameter) {$ this- & gt; Some_parameter = $ init_parameter; }} // in code: $ foo = new Foo ("some init value");
Notice - The creators of the old style are called so that you can run in legacy code. They look like this:
square fu {function fu ($ init_parameter) {$ this- & gt; Some_parameter = $ init_parameter; }}
This form has been officially downgraded to PHP 7 and you should not use it for new code.
Comments
Post a Comment