// FILE1.php
class file1 {
function hi() {
echo "hi from file1";
}
}
// FILE2.php
# In this file I am going to use file1 functions
include_once './file1.php';
class file2 {
function hibye() {
$file1 = new file1; # Importing class from file1
$file1->hi();
echo "bye from file2";
}
}
// index.php
// In this file I want to use both file1 and file2 functions
include './file1.php';
include './file2.php';
$f1 = new file1;
$f2 = new file2;
$f1->hi();
$f1->hibye();
When I run index.php file it shows error saying file1 is already imported, Yes, I know that but, I want to explicitly include file1 again in index.php file.
I think the problem might be understanding basic PHP. We don’t know the OP’s PHP skill level, but the use of include_once tells me they don’t have an understanding of why not to use it. If the OP is a beginner, then I strongly wouldn’t jump into doing OOP just yet. Learn the basics first. OOP is far more complicated than basic PHP syntax.
Maybe looking at what index.php looks like after the content of the includes is included will let you see the problem?
//include './file1.php';
class file1 {
function hi() {
echo "hi from file1";
}
}
//include './file2.php';
//include_once './file1.php';
class file1 {
function hi() {
echo "hi from file1";
}
}
class file2 {
function hibye() {
$file1 = new file1; # Importing class from file1
$file1->hi();
echo "bye from file2";
}
}
$f1 = new file1;
$f2 = new file2;
$f1->hi();
$f1->hibye();