MFHJoe Posted July 21, 2008 Share Posted July 21, 2008 Hi I'm trying to make an OOP system and it's not working the way I want it to. I've not had too much experience with OOP in PHP so it might be something obvious that's breaking it. I have 3 files: parent.php <?php class c_parent { function __construct() { include('child.php'); echo 'constructed<br />'; } function test_parent() { echo 'parent working<br />'; } } $parent = new c_parent; ?> child.php <?php class c_child extends c_parent { function test_child() { echo 'child working<br />'; } } ?> init.php <?php include('parent.php'); $parent->test_parent(); $parent->test_child(); ?> What I was hoping to achieve is being able to access every function from the child classes from the parent class variable, however I get the following feedback upon running init.php: constructed parent working Fatal error: Call to undefined method c_parent::test_child() in /Applications/MAMP/htdocs/oop/lib/init.php on line 5 This shows that the constructor class has run and the child class has been included, but the function from inside the child class still can't be used. What's the reason for this? Have I got the wrong idea of what I can do with PHP? Or is there another way to go about this? Cheers Joe Link to comment https://forums.phpfreaks.com/topic/115864-solved-oop-system-not-working/ Share on other sites More sharing options...
DarkWater Posted July 21, 2008 Share Posted July 21, 2008 Children get parent methods, not vice-versa. Link to comment https://forums.phpfreaks.com/topic/115864-solved-oop-system-not-working/#findComment-595657 Share on other sites More sharing options...
LemonInflux Posted July 21, 2008 Share Posted July 21, 2008 As said above, you'd need $child = new c_child; and call from that. Link to comment https://forums.phpfreaks.com/topic/115864-solved-oop-system-not-working/#findComment-595658 Share on other sites More sharing options...
MFHJoe Posted July 21, 2008 Author Share Posted July 21, 2008 I know it would be something like that -- cheers, both. Link to comment https://forums.phpfreaks.com/topic/115864-solved-oop-system-not-working/#findComment-595724 Share on other sites More sharing options...
DarkWater Posted July 21, 2008 Share Posted July 21, 2008 Any time. Be sure to post any new questions. Link to comment https://forums.phpfreaks.com/topic/115864-solved-oop-system-not-working/#findComment-595728 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.