using namespaces and file paths in php properly

having mutiple issues with file paths and using namespaces and not sure why it seems they wont load.
Fatal error: Uncaught Error: Class "MD\model\TodoModel" not found in /var/www/html/app/controller/TodoController.php:7 Stack trace: #0 /var/www/html/app/view/todos/index.php(4): include() #1 {main} thrown in /var/www/html/app/controller/TodoController.php on line 7
Fatal error: Uncaught Error: Class "MD\model\TodoModel" not found in /var/www/html/app/controller/TodoController.php:7 Stack trace: #0 /var/www/html/app/view/todos/index.php(4): include() #1 {main} thrown in /var/www/html/app/controller/TodoController.php on line 7
here is code
<?php
namespace MD\controller;
set_include_path('app/model/');
//require '../model/TodoModel.php';
use MD\model\TodoModel;

class TodoController extends TodoModel {
public function viewTodos() {
return $this->selectAllTodos();
}

public function add_Todo($todo, $data) {
return $this->addTodo($todo, $data);
}

public function edit_Todo($id) {
return $this->editTodo($id);
}

public function update_Todo($id, $topic, $task) {
return $this->updateTodo($id, $topic, $task);
}

public function delete_Todo($id) {
return $this->deleteTodo($id);
}
}
<?php
namespace MD\controller;
set_include_path('app/model/');
//require '../model/TodoModel.php';
use MD\model\TodoModel;

class TodoController extends TodoModel {
public function viewTodos() {
return $this->selectAllTodos();
}

public function add_Todo($todo, $data) {
return $this->addTodo($todo, $data);
}

public function edit_Todo($id) {
return $this->editTodo($id);
}

public function update_Todo($id, $topic, $task) {
return $this->updateTodo($id, $topic, $task);
}

public function delete_Todo($id) {
return $this->deleteTodo($id);
}
}
50 Replies
MD
MDā€¢9mo ago
here is model code
<?php
namespace MD\model;
use MD\config\Database;

class TodoModel extends Database {
public function addTodo($todo, $data) {
$date = date('y-m-d H-m-sa');
$done = 0;

$sql = "INSERT INTO `todo`(`topic`, `task`, `is_done`, `add_date`)VALUE(?,?,?,?)";
$stmt = $this->connect()->prepare($sql);
return $stmt->execute([$todo, $data, $done, $date]);
}

public function selectAllTodos() {
$sql = "SELECT * FROM `todo`";
$stmt = $this->connect()->prepare($sql);
$stmt->execute();
$results = $stmt->fetchAll();

return $stmt;
}

public function editTodo($id) {
$sql = "SELECT * FROM `todo` WHERE id=?";
$stmt = $this->connect()->prepare($sql);
$stmt->execute([$id]);

$result = $stmt->fetch();
return $result;
}

public function updateTodo($id, $topic, $data) {
$sql = "UPDATE `todo` SET topic = ?, task = ?, WHERE id = ?";
$stmt = $this->connect()->prepare($sql);
$result = $stmt->execute([$topic, $data, $id]);

header("location:index.php");
return $result;
}

public function deleteTodo($id) {
$sql = "DELETE FROM `todo` WHERE id=?";
$stmt = $this->connect()->prepare($sql);
$result = $stmt->execute([$id]);

return $result;
}
}
<?php
namespace MD\model;
use MD\config\Database;

class TodoModel extends Database {
public function addTodo($todo, $data) {
$date = date('y-m-d H-m-sa');
$done = 0;

$sql = "INSERT INTO `todo`(`topic`, `task`, `is_done`, `add_date`)VALUE(?,?,?,?)";
$stmt = $this->connect()->prepare($sql);
return $stmt->execute([$todo, $data, $done, $date]);
}

public function selectAllTodos() {
$sql = "SELECT * FROM `todo`";
$stmt = $this->connect()->prepare($sql);
$stmt->execute();
$results = $stmt->fetchAll();

return $stmt;
}

public function editTodo($id) {
$sql = "SELECT * FROM `todo` WHERE id=?";
$stmt = $this->connect()->prepare($sql);
$stmt->execute([$id]);

$result = $stmt->fetch();
return $result;
}

public function updateTodo($id, $topic, $data) {
$sql = "UPDATE `todo` SET topic = ?, task = ?, WHERE id = ?";
$stmt = $this->connect()->prepare($sql);
$result = $stmt->execute([$topic, $data, $id]);

header("location:index.php");
return $result;
}

public function deleteTodo($id) {
$sql = "DELETE FROM `todo` WHERE id=?";
$stmt = $this->connect()->prepare($sql);
$result = $stmt->execute([$id]);

return $result;
}
}
directory structure
MD
MDā€¢9mo ago
No description
b1mind
b1mindā€¢9mo ago
Why MD/ ? also never capitalize paths MD/ is your project folder? or is that some part of the namespace I'm just not keen to?
set_include_path('app/model/'); //require '../model/TodoModel.php';
You realize both these paths are not even the same right?
MD
MDā€¢9mo ago
actually I removed those
MD
MDā€¢9mo ago
took out all the md
b1mind
b1mindā€¢9mo ago
why did you even have it xD that makes me think you don't understand paths at all mate typically I don't think you would even be calling for /app/ but you would want to do /app being at the root/ of your project
MD
MDā€¢9mo ago
idk how php handles that because it seems different in go it was very easy in php it's not
b1mind
b1mindā€¢9mo ago
again... paths are paths man
MD
MDā€¢9mo ago
in php you have namespaces, require, include,
b1mind
b1mindā€¢9mo ago
so what
MD
MDā€¢9mo ago
ive tried them all and nothing has worked
Fatal error: Uncaught Error: Class "model\TodoModel" not found in /var/www/html/app/controller/TodoController.php:5 Stack trace: #0 /var/www/html/app/view/todos/index.php(4): include() #1 {main} thrown in /var/www/html/app/controller/TodoController.php on line 5
Fatal error: Uncaught Error: Class "model\TodoModel" not found in /var/www/html/app/controller/TodoController.php:5 Stack trace: #0 /var/www/html/app/view/todos/index.php(4): include() #1 {main} thrown in /var/www/html/app/controller/TodoController.php on line 5
b1mind
b1mindā€¢9mo ago
What are you trying now since you changed the above
MD
MDā€¢9mo ago
that's closest I got so far
<?php
namespace controller;
use model\TodoModel;

class TodoController extends TodoModel {
public function viewTodos() {
return $this->selectAllTodos();
}

public function add_Todo($todo, $data) {
return $this->addTodo($todo, $data);
}

public function edit_Todo($id) {
return $this->editTodo($id);
}

public function update_Todo($id, $topic, $task) {
return $this->updateTodo($id, $topic, $task);
}

public function delete_Todo($id) {
return $this->deleteTodo($id);
}
}
<?php
namespace controller;
use model\TodoModel;

class TodoController extends TodoModel {
public function viewTodos() {
return $this->selectAllTodos();
}

public function add_Todo($todo, $data) {
return $this->addTodo($todo, $data);
}

public function edit_Todo($id) {
return $this->editTodo($id);
}

public function update_Todo($id, $topic, $task) {
return $this->updateTodo($id, $topic, $task);
}

public function delete_Todo($id) {
return $this->deleteTodo($id);
}
}
it says it cant find "model\TodoModel"
<?php
namespace model;
use config\Database;

class TodoModel extends Database {
public function addTodo($todo, $data) {
$date = date('y-m-d H-m-sa');
$done = 0;

$sql = "INSERT INTO `todo`(`topic`, `task`, `is_done`, `add_date`)VALUE(?,?,?,?)";
$stmt = $this->connect()->prepare($sql);
return $stmt->execute([$todo, $data, $done, $date]);
}

public function selectAllTodos() {
$sql = "SELECT * FROM `todo`";
$stmt = $this->connect()->prepare($sql);
$stmt->execute();
$results = $stmt->fetchAll();

return $stmt;
}

public function editTodo($id) {
$sql = "SELECT * FROM `todo` WHERE id=?";
$stmt = $this->connect()->prepare($sql);
$stmt->execute([$id]);

$result = $stmt->fetch();
return $result;
}

public function updateTodo($id, $topic, $data) {
$sql = "UPDATE `todo` SET topic = ?, task = ?, WHERE id = ?";
$stmt = $this->connect()->prepare($sql);
$result = $stmt->execute([$topic, $data, $id]);

header("location:index.php");
return $result;
}

public function deleteTodo($id) {
$sql = "DELETE FROM `todo` WHERE id=?";
$stmt = $this->connect()->prepare($sql);
$result = $stmt->execute([$id]);

return $result;
}
}
<?php
namespace model;
use config\Database;

class TodoModel extends Database {
public function addTodo($todo, $data) {
$date = date('y-m-d H-m-sa');
$done = 0;

$sql = "INSERT INTO `todo`(`topic`, `task`, `is_done`, `add_date`)VALUE(?,?,?,?)";
$stmt = $this->connect()->prepare($sql);
return $stmt->execute([$todo, $data, $done, $date]);
}

public function selectAllTodos() {
$sql = "SELECT * FROM `todo`";
$stmt = $this->connect()->prepare($sql);
$stmt->execute();
$results = $stmt->fetchAll();

return $stmt;
}

public function editTodo($id) {
$sql = "SELECT * FROM `todo` WHERE id=?";
$stmt = $this->connect()->prepare($sql);
$stmt->execute([$id]);

$result = $stmt->fetch();
return $result;
}

public function updateTodo($id, $topic, $data) {
$sql = "UPDATE `todo` SET topic = ?, task = ?, WHERE id = ?";
$stmt = $this->connect()->prepare($sql);
$result = $stmt->execute([$topic, $data, $id]);

header("location:index.php");
return $result;
}

public function deleteTodo($id) {
$sql = "DELETE FROM `todo` WHERE id=?";
$stmt = $this->connect()->prepare($sql);
$result = $stmt->execute([$id]);

return $result;
}
}
that's model folder with TodoModel both controller and model folders are in the app folder trying include ../model/TodoModel.php, require ../model/TodoModel.php and use model\TodoModel all yield the same error if trying set_include_path("app/model/") and others have failed even full paths didnt work
b1mind
b1mindā€¢9mo ago
omg mate... you really don't understand paths have you tried /app/model? šŸ˜‰ like I said
MD
MDā€¢9mo ago
yes it failed
b1mind
b1mindā€¢9mo ago
/model/filename?
MD
MDā€¢9mo ago
yep
b1mind
b1mindā€¢9mo ago
looks like you need to learn how then šŸ˜„ you do know that ../ and are not the same right? and / is root?
MD
MDā€¢9mo ago
yes I use linux daily
b1mind
b1mindā€¢9mo ago
yea I know that is why I'm laughing cause I would think you know this shit
MD
MDā€¢9mo ago
well php is different
b1mind
b1mindā€¢9mo ago
paths are paths man idk how many times I gotta say this
MD
MDā€¢9mo ago
if remove use model\TodoModel; this then the extends fails if you try any combination of include, or require with the path to the folder it still can't find it you get errors like this
b1mind
b1mindā€¢9mo ago
So you have to import the correct path b4 use will work ya? LIke you have to give the namespace the correct path before you can use its shorthand
MD
MDā€¢9mo ago
Warning: include(app/model/): Failed to open stream: No such file or directory in /var/www/html/app/controller/TodoController.php on line 3

Warning: include(): Failed opening 'app/model/' for inclusion (include_path='.:/usr/local/lib/php') in /var/www/html/app/controller/TodoController.php on line 3

Fatal error: Uncaught Error: Class "model\TodoModel" not found in /var/www/html/app/controller/TodoController.php:6 Stack trace: #0 /var/www/html/app/view/todos/index.php(4): include() #1 {main} thrown in /var/www/html/app/controller/TodoController.php on line 6
Warning: include(app/model/): Failed to open stream: No such file or directory in /var/www/html/app/controller/TodoController.php on line 3

Warning: include(): Failed opening 'app/model/' for inclusion (include_path='.:/usr/local/lib/php') in /var/www/html/app/controller/TodoController.php on line 3

Fatal error: Uncaught Error: Class "model\TodoModel" not found in /var/www/html/app/controller/TodoController.php:6 Stack trace: #0 /var/www/html/app/view/todos/index.php(4): include() #1 {main} thrown in /var/www/html/app/controller/TodoController.php on line 6
there's an example
b1mind
b1mindā€¢9mo ago
right cause where is it looking for model\TodoModel.... you calling it from inside /app/controller yes?
MD
MDā€¢9mo ago
you cant use backslashes in keyword use it only allows forward
b1mind
b1mindā€¢9mo ago
so its looking for /app/controller/model/TodoModel but you want /app/model/ Am I wrong in seeing this ?
MD
MDā€¢9mo ago
no that is what im trying to achieve but the way php does things im not reaching it correctly
b1mind
b1mindā€¢9mo ago
wait I read that wrong so its doing this
MD
MDā€¢9mo ago
use model/TodoModel yields an unexpected ; error so syntax fail
b1mind
b1mindā€¢9mo ago
/app/controller/app/model xD
MD
MDā€¢9mo ago
../ would take you one level out of controllers putting you in app which has model folder in it
b1mind
b1mindā€¢9mo ago
do you need to define __DIR__./model/ or something?
MD
MDā€¢9mo ago
yet php doesnt acknowledge it ive tried that too
b1mind
b1mindā€¢9mo ago
Opensource.com
Use autoloading and namespaces in PHP
PHP autoloading and namespaces provide handy conveniences with huge benefits.
MD
MDā€¢9mo ago
read that too
b1mind
b1mindā€¢9mo ago
Scroll down the PSR-4 bit
MD
MDā€¢9mo ago
you need composer
b1mind
b1mindā€¢9mo ago
Well mate idk what to tell you don't use namespaces then
MD
MDā€¢9mo ago
it's part of php now so it's needed
b1mind
b1mindā€¢9mo ago
All of these examples use / root paths though
MD
MDā€¢9mo ago
im used to the php 5 days this is way different it's like Java now usually I dont have trouble with paths this is just a weird one
b1mind
b1mindā€¢9mo ago
its not weird you just have to fingure out your mental block paths work the same anywhere You just need to know how it handles the paths šŸ˜„ Like these do show it going outside of app too right? idk hf with this šŸ˜‚ glad its not me šŸ––
MD
MDā€¢9mo ago
idk either this language has changed a lot and ive only used php 3 times in my life mainly it's Java or C
b1mind
b1mindā€¢9mo ago
yea mate.. your name spaces look like they need to start with \namespace from all the examples I see the whole / to \ drives me a bit nuts šŸ¤£
MD
MDā€¢9mo ago
yeah php enforces \ on namespaces
b1mind
b1mindā€¢9mo ago
yea idk man I can't be of anyhelp as I don't know it well enough but seems pretty clear cut šŸ¤·ā€ā™‚ļø I would try an example from one these in your project Get it working first then make your own šŸ˜‰
MD
MDā€¢9mo ago
maybe jochem or epic will provide insight because im not gonna make any progress til this works this could be all day epic showed me with composer that's what i know šŸ˜„ just never setup composer with php and apache in docker lord even composer failed yeah even in the same damn folder it cant find it that's beyond me
Want results from more Discord servers?
Add your server