I have the following question:
This is my main file index.php.
<?php
class myClass
{
function myClass()
{
echo '[constructor]';
$this->myVar = '[i am making a test]';
$this->A();
}
function A()
{
echo '[function A works too]';
echo $this->myVar;
}
}
$test = new myClass;
?>
Now I need to move function A into another PHP file includes/function_a.php which I could include_once in the main file. The reason is to make my main file smaller and easier to read.
What is the best practice of having this done, EXTENDS?
EDIT:
I’ve done this using EXTENDS:
index.php
<?php
include_once('includes/function_a.php');
class myClass extends anotherClass
{
function myClass()
{
echo '[constructor]';
$this->myVar = '[i am making a test]';
$this->A();
}
}
$test = new myClass;
?>
includes/function_a.php
<?php
class anotherClass
{
function A()
{
echo '[function A works too]';
echo $this->myVar;
}
}
?>
Any better ideas?
You hint at the best way to do it at the end of your question. You would need to create a parent class and have your “myClass” extend your parent class. “myClass” would then inherit the methods of the parent class and the parent class could be in another file. Is that what you were looking for?