i'm getting the next error:
Error: Class 'App\JL\SplFileInfo' not found while trying to create a SplFileInfo class like this: $i = new SplFileInfo($P['Carpeta'].DS.$v);
i tested this with same results: $i = new SplFileInfo('');
this is in a cutom class called JL in namespace App\JL and calling it like use App\JL\JL;
The function is declared like this: public static function LeerArchivos(&$P = array()) {
i'm working in an element to be shown on a template...
¿Do i have to activate somethig? or ¿What am i doing wrong?
The problem is that you don't know how namespaces work. You should fix that gap in your knowledge first: http://php.net/manual/en/language.namespaces.php
You need to "import" the class from the "root" namespace \ into your current namespace \App\JL\JL. This us done via the use keyword. See the documentation for it.
Related
I have a class for dynamoDB in location
src/Dynamo/shop.php
My class looks like below
<?php
namespace App\Dynamo;
class Shop
{
-----
}
?>
I am trying to implement unit test for this class, So I have created a folder class dynamo in below location.
app/cake/tests/TestCase/Dynamo
In Dynamo folder I have created a class with file name ShopTest.php
For create a unit testing I have written this class like below
<?php
declare(strict_types=1);
namespace App\Test\TestCase\Dynamo;
use App\Dynamo\Shop;
use Cake\TestSuite\TestCase;
class ShopTest{
public function setUp()
{
$this->shop = new Shop;
}
public function testconnectDynamoDB()
{
debug($this->shop->connectDynamoDB());
$this->assertNotEmpty($this->shop->connectDynamoDB());
}
}
Now after run phpunit command
vendor/bin/phpunit tests/TestCase/Dynamo/ShopTest.php
I am getting
Class 'App\Test\TestCase\Dynamo\ShopTest' could not be found in '/var/www/html/tests/TestCase/Dynamo/ShopTest.php'.
Class is present in this location , Why I am getting Class could not be found ?
root#0ceda1df4444:/var/www/html# cd /var/www/html/tests/TestCase/Dynamo/
root#0ceda1df4444:/var/www/html/tests/TestCase/Dynamo# ls
ShopTest.php
root#0ceda1df4444:/var/www/html/tests/TestCase/Dynamo# cat ShopTest.php
<?php
declare(strict_types=1);
namespace App\Test\TestCase\Dynamo;
use App\Dynamo\Shop;
use Cake\TestSuite\TestCase;
class ShopTest{
Also I am trying to run all test case by below command , I am getting a warning.
root#0ceda1df4444:/var/www/html# vendor/bin/phpunit
PHPUnit 9.5.9 by Sebastian Bergmann and contributors.
Warning: Your XML configuration validates against a deprecated schema.
Suggestion: Migrate your XML configuration using "--migrate-configuration"!
No tests executed!
There is no test class
The error message there is a bit confusing, but the crux is this is not a test class:
<?php
declare(strict_types=1);
namespace App\Test\TestCase\Dynamo;
use App\Dynamo\Shop;
use Cake\TestSuite\TestCase;
class ShopTest{ # <--
It is just a class (which coincidentally has the word Test in its name).
Compare to an example from the documentation:
namespace App\Test\TestCase\View\Helper;
use App\View\Helper\ProgressHelper;
use Cake\TestSuite\TestCase;
use Cake\View\View;
class ProgressHelperTest extends TestCase # <--
{
To be detected as a test the class must extend TestClass - therefore to correct this:
...
use App\Dynamo\Shop;
use Cake\TestSuite\TestCase;
class ShopTest extends TestCase # <--
{
With that change the test class will load and some easier-to-solve problems will become apparent:
$ vendor/bin/phpunit tests/TestCase/Dynamo/ShopTest.php
PHP Fatal error: Declaration of ShopTest::setUp() must be compatible with Cake\TestSuite\TestCase::setUp(): void in ~/repos/cakephp/app/tests/TestCase/Dynamo/ShopTest.php on line 11
Fatal error: Declaration of ShopTest::setUp() must be compatible with Cake\TestSuite\TestCase::setUp(): void in ~/repos/cakephp/app/tests/TestCase/Dynamo/ShopTest.php on line 11
i created custom classes in the plugin. This class contains several methods. Unfortunately sometimes crashes on Exception
Error: Fatal Error (1): Call to undefined method DateTools::getUserDateFromSQL() in ...
but this method is in the class :(. Unfortunately an error occurs infrequently and can not even repeat it.
My class in plugin Tools (folder Lib)
class DateTools {
public function getUserDateFromSQL($value) {
if(empty($value)) return $value;
return Date("d.m.Y H:i", $this->getDateFromSQLDate($value));
}
...
...
...
The place where source crash.
App::import('DateTools', 'Tools.Lib');
class SomeController extends SomeParentAppController {
public function someMethod($arraysDate) {
$dateTools = new DateTools();
$result[$key] = $dateTools->getUserDateFromSQL($value);
...
...
...
Thanks for help
The syntax used to include the class is incorrect
You could use App::uses instead of App::import
App::uses('DateTools', 'Tools.Lib');
Syntax for App::import()
Syntax for App::uses()
I'm adding an external class to a cake 3.0 app by putting it to /vendor/name folder and requiring it from a component like this:
require_once( $_SERVER['DOCUMENT_ROOT'].'/project/vendor/external/testClass.php');
But when I try to getInstance(); of a class - I get an error
Class 'App\Controller\Component\Test_Class' not found
I am calling this from a component (thus the \Controller\Component).
What is it that i'm doing wrong?
CakePHP 3.0 uses namespaces. So use proper namespace for your vendor class or if it's not using namespaces prefix the class name with backslash when using it.
E.g. $object = new \Test_Class();.
I'm trying to use CakePHP's HttpSocket class to make an API call, but I can't seem to get it to include the class. It seems pretty simple, so I can't see where I might be going wrong, but here's what I have:
At the top of the controller:
class RetailersController extends AppController {
public $uses = array(...lots of classes..., 'HttpSocket', 'Network/Http');
Then in the function itself:
$HttpSocket = new HttpSocket();
And when I run that, I get:
Fatal Error
Error: Class 'HttpSocket' not found
As I said, there's not much to this, so I can't see that there's much that can go wrong - but I seem to have managed it! What can I try next?
I'm using CakePHP 2.4.
Add this before class definition:
App::uses('HttpSocket', 'Network/Http');
This variable is only for models: $uses
I want to execute a static method from certain class in certain namespace, but I have a problem with using it as a method parameter.
Example:
Lets say there is a class:
namespace ExampleNamespace {
public ref class A
{
public:
static int MethodA();
};
}
And I want to use MethodA in other namespace as a other's method parameter:
MethodB(MethodA());
Only way I can make it work is by writing it like this:
ExampleNamespace::A^ a;
MethodB(a->MethodA());
Is there a way to write it without that 'a' declaration before?
Something like
MethodB(ExampleNamespace::A->MethodA())
wont work...
Thank you in advance.
MethodB(ExampleNamespace::A::MethodA());