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(); } } ?> Link to comment https://forums.phpfreaks.com/topic/114144-solved-calling-functions/ 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. =/ Link to comment https://forums.phpfreaks.com/topic/114144-solved-calling-functions/#findComment-586696 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. Link to comment https://forums.phpfreaks.com/topic/114144-solved-calling-functions/#findComment-586698 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? Link to comment https://forums.phpfreaks.com/topic/114144-solved-calling-functions/#findComment-586702 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. Link to comment https://forums.phpfreaks.com/topic/114144-solved-calling-functions/#findComment-586707 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; Link to comment https://forums.phpfreaks.com/topic/114144-solved-calling-functions/#findComment-586708 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? Link to comment https://forums.phpfreaks.com/topic/114144-solved-calling-functions/#findComment-586712 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.