webref.eu Posted February 12, 2010 Share Posted February 12, 2010 Hi All How do you pass a variable value into a constructor? When a class is being included in a page, and that class contains a constructor, I don't get how you can send variable values into that constructor. I know how to send a variable value into a function of an instantiated object, but this is different ... Thanks Quote Link to comment https://forums.phpfreaks.com/topic/191892-how-do-you-pass-a-variable-value-into-a-constructor/ Share on other sites More sharing options...
wildteen88 Posted February 12, 2010 Share Posted February 12, 2010 The constructor can take arguments just like functions. class My_Class { function __construct($var1, $var2) { } } $myClass = new My_Class('value1', 'value2'); Quote Link to comment https://forums.phpfreaks.com/topic/191892-how-do-you-pass-a-variable-value-into-a-constructor/#findComment-1011444 Share on other sites More sharing options...
roopurt18 Posted February 12, 2010 Share Posted February 12, 2010 I know how to send a variable value into a function of an instantiated object, but this is different ... Actually it's exactly the same! Quote Link to comment https://forums.phpfreaks.com/topic/191892-how-do-you-pass-a-variable-value-into-a-constructor/#findComment-1011447 Share on other sites More sharing options...
webref.eu Posted February 12, 2010 Author Share Posted February 12, 2010 OK, thanks guys, but how does this work when the class is being called with: require_once('class.classname.php'); How can I send the variable values when require_once is being used? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/191892-how-do-you-pass-a-variable-value-into-a-constructor/#findComment-1011459 Share on other sites More sharing options...
Maq Posted February 12, 2010 Share Posted February 12, 2010 OK, thanks guys, but how does this work when the class is being called with: require_once('class.classname.php'); How can I send the variable values when require_once is being used? Thanks You pass the value(s) when you create the object, not when you import the file. Quote Link to comment https://forums.phpfreaks.com/topic/191892-how-do-you-pass-a-variable-value-into-a-constructor/#findComment-1011461 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.