Jump to content

[SOLVED] OOP system not working


MFHJoe

Recommended Posts

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.