jaikar Posted March 17, 2007 Share Posted March 17, 2007 Hi, is it possible to create an object inside a class?... say i have 2 class... class a { var $val; some code } class b { $myobject = new a; $myobject->val=1; ............... } will the above work ??... first of all .. is it possible to do like above ?.... Please advice..... in my code i used something like this but its not working.. Jaikar Quote Link to comment https://forums.phpfreaks.com/topic/43142-creating-an-object-inside-a-class/ Share on other sites More sharing options...
najibk Posted March 17, 2007 Share Posted March 17, 2007 Yeah what you have done should definitely work. If it's not working, post some code and maybe we can suggest what may be going wrong. Also, make sure you PHP version is not something that isn't too old. I wouldn't necessarily recommend using anything object oriented in PHP 3 or earlier. Quote Link to comment https://forums.phpfreaks.com/topic/43142-creating-an-object-inside-a-class/#findComment-209518 Share on other sites More sharing options...
idevlabsdotcom Posted March 17, 2007 Share Posted March 17, 2007 it should work fine. this code outputs "WOOT": <?php $test = new bar("WOOT"); class foo { var $test; function foo($test){ $this->test = $test; } function print_object(){ echo $this->test; } } class bar { var $object; function bar($test){ $this->object = new foo($test); $this->object->print_object(); } } ?> post your code so we can give you some guidance. Quote Link to comment https://forums.phpfreaks.com/topic/43142-creating-an-object-inside-a-class/#findComment-209527 Share on other sites More sharing options...
jaikar Posted March 17, 2007 Author Share Posted March 17, 2007 Thanks najibk , idevlabsdotcom .... my code was similar to what idevlabsdotcom gave... now i can say i made a mistake.... but why $this->test = $test is inside a function ?... why not set outside the function.. what is where i got the error.... i set the value outside the function.. like class foo { var $test; $this->test = $test; ???????????????// function print_object(){ echo $this->test; } } why i am getting the error if i do thin..... ??? Quote Link to comment https://forums.phpfreaks.com/topic/43142-creating-an-object-inside-a-class/#findComment-209537 Share on other sites More sharing options...
jaikar Posted March 17, 2007 Author Share Posted March 17, 2007 can anyone please advice about this.. why $this->test=$test .. should in a function .. y not outside the function ? Thanks !.. Quote Link to comment https://forums.phpfreaks.com/topic/43142-creating-an-object-inside-a-class/#findComment-209616 Share on other sites More sharing options...
jaikar Posted March 18, 2007 Author Share Posted March 18, 2007 can anyone explain ... why in a class this declaration $this->test=$test should be in a fuction .. why not outside the function... Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/43142-creating-an-object-inside-a-class/#findComment-209915 Share on other sites More sharing options...
trq Posted March 18, 2007 Share Posted March 18, 2007 Because it makes no sense outside of a function. You can't really assign anything to it outside of a method. Quote Link to comment https://forums.phpfreaks.com/topic/43142-creating-an-object-inside-a-class/#findComment-209921 Share on other sites More sharing options...
ignace Posted March 18, 2007 Share Posted March 18, 2007 well actually that is a very good question xD, it probably has something to do with those 0's and 1's called bits xD, and ofcourse the class scope which only allows to declare properties (which represent your variables) or methods (which represent functions) for more information on programming in OOP: http://www.phpfreaks.com/tutorials/150/0.php Quote Link to comment https://forums.phpfreaks.com/topic/43142-creating-an-object-inside-a-class/#findComment-209928 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.