Hey all,
I am trying to use custom classes globally, so i setup a TestClass.php with a public test function() returning a simple string.
Just for testing before i can continue.
For this to work i created a folder with php file in:
“packages/fishueel_package/src/TestClass/TestClass.php”
The code looks like:
<?php
namespace Concrete\Package\FishueelPackage\Src\TestClass;
class TestClass {
public function test() {
return "Test";
}
}
?>
And from another file, inside:
“packages/fishueel_package/themes/fishueel”
I try to get/use the class using:
use \Concrete\Package\FishueelPackage\Src\TestClass\TestClass;
$testVar = TestClass::test();
But everytime i try to use it like the code above i get an error: “HTTP ERROR 500”
I searched the forums and google hoping to find a solution, but without succes.
Could anyone help me out, i thought this would be a bit easier after reading several posts online on stackoverflow.
Cheers!
Rather than living in the Concrete namespace, try setting up your own package specific namespace
https://marketplace.concretecms.com/documentation/developers/5.7/packages/adding-custom-code-to-packages/
See pkgAutoloaderRegistries
Okay, so i am probably misreading something or i dont understand it at all.
As far as i understand, i need to put the following:
protected $pkgAutoloaderRegistries = array(
'src/TestClass' => '\TestClass'
);
In the controller.php of the package.
Change the namespace in the folder/class: “packages/fishueel_package/src/TestClass/TestClass.php”
from:
“namespace Concrete\Package\FishueelPackage\Src\TestClass;”
to:
“TestClass”
And place the function:
public function on_start()
{
$listener = \TestClass();
$listener->addListeners(); //is this a Concrete function?
}
In the controller.php of the package as well, however whenever i insert the on_start() function its giving me the error:
“Call to undefined function TestClass()”
And without the on_start() function, i still cant seem to get variables or call the class from another file.
Not quite
No need for the on_start().
The namespace maps to a folder, not to the actual class/file
protected $pkgAutoloaderRegistries = array(
'src/TestClasses' => '\TestClassses'
);
folder
my_package/src/TestClasses/
File
/src/TestClasses/MyTestClass.php
Namespace TestClassses;
class MyTestClass {...}
You can create further classes and nest namespaces beneath \TestClasses\ without further declaration.
Thank you very much! Its a little bit clearer now. I got it to work thanks to your help!
1 Like