aim25 Posted July 10, 2008 Share Posted July 10, 2008 Is it possible to call a function within another function? Example: <?php class MAIN { function A() { .. some code } } class SOMETHING extends MAIN { function B() { $var = new MAIN; $var->A(); } } ?> Quote Link to comment Share on other sites More sharing options...
DarkWater Posted July 10, 2008 Share Posted July 10, 2008 Uhh.....yes? Have you ever done: function truncate($string, $length=50) { return substr($string, 0, $length); } Or something like that? O_O Really strange question. =/ Quote Link to comment Share on other sites More sharing options...
aim25 Posted July 10, 2008 Author Share Posted July 10, 2008 That doesn't really answer the question... Sorry. Quote Link to comment Share on other sites More sharing options...
DarkWater Posted July 10, 2008 Share Posted July 10, 2008 That completely answers the question. I said yes. I've just never heard anyone ask that question...Why wouldn't you be able to call a function in another function? Quote Link to comment Share on other sites More sharing options...
aim25 Posted July 10, 2008 Author Share Posted July 10, 2008 My bad, the i focused on teh code you provided. Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted July 10, 2008 Share Posted July 10, 2008 Yes you can do that, however when you do $var = new MAIN you're creating a new instance of the main class. If all you want to do is call function A from the function B in SOMETHING class then do parent::A() instead; Quote Link to comment Share on other sites More sharing options...
DarkWater Posted July 10, 2008 Share Posted July 10, 2008 Yes you can do that, however when you do $var = new MAIN you're creating a new instance of the main class. If all you want to do is call function A from the function B in SOMETHING class then do parent::A() instead; Yes, if you just want to call the function with the current instance of B that you are using, you'll use the parent:: scope. If you actually want a new instance, then you can do it your way. What are you trying to accomplish? Quote Link to comment 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.