Is it possible to put my helper functions in a namespace?
My current setup (which I can't get working) is:
app\Helpers\simple_html_dom.php:
<?php namespace App\Helpers\HtmlDomParser; function file_get_html(){ echo 'file_get_html() called'; }
composer.json
"autoload": { "files": [ "app/Helpers/simple_html_dom.php", "app/Helpers/common.php" ], "psr-4": { "App\": "app/", "Database\Factories\": "database/factories/", "Database\Seeders\": "database/seeders/" } },
app\Services\dde\dde_trait.php
<?php namespace App\Services\dde; use App\Helpers\HtmlDomParser; trait ddeTrait{ public function parse(){ $content = HtmlDomParser::file_get_html(); } }
The error I receive is The class "App\Helpers\HtmlDomParser" cannot be found.
But HtmlDomParser is not a class, but a namespace.
Is the only correct setting to put the file_get_html()
function into the HtmlDomParser class? Laravel version: 8
P粉8264299072024-03-28 10:40:47
You can do like this. 1- remove namespace from helper file. app\Helpers\simple_html_dom.php:reply0
P粉0557261462024-03-28 00:25:53
You did not define the class "HtmlDomParser", only the namespace "App\Helpers\HtmlDomParser". To call functions in this namespace, use the fully qualified version:
App\Helpers\HtmlDomParser\file_get_html().
You can check this page: https://www.php .net/manual/en/language.namespaces.rules.php