Require a class from another class - php
I'm learning PHP. And I have a problem that I can't understand. This my situation:
I have a directory called Lib. This is the structure of my directory:
In the directory database I wrote a class QueryBuilder.
<?php
//require '../Task';
class QueryBuilder{
public function selectAll($table,$pdo){
$statement = $pdo->prepare("Select * from {$table}");
$statement->execute();
return $statement->fetchAll(PDO::FETCH_CLASS,'Task');
}
}
?>
In the class I use an external class Task that I wrote before and that I use in the statement return.
return $statement->fetchAll(PDO::FETCH_CLASS,'Task');
This is the Task class:
<?php
class Task{
protected $description;
protected $completed=false;
public function __contruct($description){
$this->description = $description;
}
public function getDescription(){
return $this->description;
}
public function getCompleted(){
return $this->completed;
}
}
?>
So I thought that If I wanted to use the Task class I had to import the file. So at the beginning of the my class QueryBuilder I wrote the require statement.
require '../Task';
The problem is that when I run my class I get this error:
Warning: require(../Task): failed to open stream: No such file or directory in C:\Users\Administrator\OneDrive - Sogei\Desktop\php-learning\php-course\Lib\database\QueryBuilder.php on line 3
Fatal error: require(): Failed opening required '../Task' (include_path='C:\xampp\php\PEAR') in C:\Users\Administrator\OneDrive - Sogei\Desktop\php-learning\php-course\Lib\database\QueryBuilder.php on line 3
Without the require statement instead the class works.
How is it possible? Is not necessary import the Task class?

Comments
Post a Comment