I want a base class to be extended, but I have some errors coming out:
Fatal error: Class 'Api\Services\Base' not found in
/var/www/html/Api/Services/Example.php on line 7
I searched for typos, tried to use the fully qualified name, made the abstract class empty or just defined it as a simple class; none of these helped.
Using "require" instead of "use" worked, but still...
Any idea (the two files are in the same directory: /var/www/html/Api/Services)?
Thanks in advance...
<?php
// Base.php
namespace Api\Services;
use Api\Classes\ErrorHandler;
use Api\Classes\ErrorMessage;
abstract class Base
{
public $data = null;
public function getData()
{
return $this->data;
}
public function setData($data = null)
{
$this->data = $data;
}
}
?>
<?php
// Example.php
namespace Api\Services;
use Api\Services\Base;
class Example extends Base
{
public $request = array();
public function __construct($request = array())
{
$this->request = $request;
}
}
?>
use Base
instead of
use Api\Services\Base;
because you are already inside the namespace Api\Services
Actually, you don't even have to write the use statement, you are inside the namespace, you can just call the classes inside the same namespace without including them (use)
Related
I have the following class
namespace MyApp;
use MyApp\SomeInterface;
class MyClass
{
public function __construct(SomeInterface $s)
{
//Some Logic here
}
//Another methods implemented There
}
The SomeInterface contains the following:
namespace MyApp
interface SomeInterface
{
/**
* #return SomeObject
*/
public function someMethodToImpement();
}
And I want to create a mock over my phpunit Test Class:
namespace Tests\MyApp;
use PHPUnit\Framework\TestCase;
use MyApp\MyClass;
use MyApp\SomeInterface;
class MyClassTest extends TestCase
{
public function someTest()
{
$fakeClass=new class{
public function myFunction($arg1,$arg2)
{
//Dummy logic to test if called
return $arg1+$arg2;
}
};
$mockInterface=$this->createMock(SomeInterface::class)
->method('someMethodToImpement')
->will($this->returnValue($fakeClass));
$myActualObject=new MyClass($mockInterface);
}
}
But Once I run it I get the error:
Tests\MyApp\MyClassTest::someTest
TypeError: Argument 1 passed to MyApp\MyClass::__construct() must implement interface MyApp\SomeInterface, instance of PHPUnit\Framework\MockObject\Builder\InvocationMocker given, called in /home/vagrant/code/tests/MyApp/MyClassTest.php on line
Do you know why that happens and how actually will create the mock Interface?
Instead of constructing the mock via
$mockInterface=$this->createMock(SomeInterface::class)
->method('someMethodToImpement')->will($this->returnValue($fakeClass));
Split it into seperate lines:
$mockInterface=$this->createMock(SomeInterface::class);
$mockInterface->method('someMethodToImpement')->will($this->returnValue($fakeClass));
And will work like a charm.
I've had a similar issue. I fixed it by adding those interfaces as another mock() parameter
class Product implements PriceInterface, ProductDataInterface {
// ...
}
Test:
// throws error
$product = Mockery::mock(Product::class);
// works fine
$product = Mockery::mock(Product::class, 'PriceInterface, ProductDataInterface');
Link to documentation
is it possible to do something like this in php? I want to have a namespace in a member variable and just always be able to call every static method of that class like I'm doing below.
Of course my code doesn't work, but I'm just wondering if that is possible at all and that I'm close to a solution, or if that's completely out of the question and must always use the syntax:
\Stripe\Stripe::setApiKey(..);
Similar question for clarifications
NOTE: I cannot modify the Stripe class, it's important it stays untouched for when future devs must update the Stripe API
Simplified code:
class StripeLib
{
var $stripe;
public function __construct()
{
// Put the namespace in a member variable
$this->stripe = '\\'.Stripe.'\\'.Stripe;
}
}
$s = new StripeLib();
// Call the static setApiKey method of the Stripe class in the Stripe namespace
$s->stripe::setApiKey(STRIPE_PRIVATE_KEY);
Yes something like this is possible. There is is static class method which can be called which returns the namespace path of the class.
<?php
namespace Stripe;
Class Stripe {
public static function setApiKey($key){
return $key;
}
}
class StripeLib
{
public $stripe;
public function __construct()
{
// Put the namespace in a member variable
$this->stripe = '\\'.Stripe::class;
}
}
$s = (new StripeLib())->stripe;
// Call the static setApiKey method of the Stripe class in the Stripe namespace
echo $s::setApiKey("testKey"); //Returns testkey
I just tested it, yes you can do that in php.
But I think you violate Dependency Injection principle here.
The right way to do that is:
class StripeLib
{
var $stripe;
// make sure Stripe implements SomeInterface
public function __construct(SomeInterface $stripe)
{
// Stripe/Stripe instance
$this->stripe = $stripe;
}
}
Currently I'm trying to call a function from a string.
This is the function, that I'll call later:
<?php
namespace App\Validation\Options;
class FacebookOptionValidation
{
static public function validate()
{
echo: 'example';
die();
}
}
Here is my controller:
<?php
namespace App\Http\Controllers\Profile;
use App\Validation\Options;
class ProfileUserEditController extends Controller {
public function updateUserOption()
{
$class = 'Options\FacebookOptionValidation';
$class::validate();
}
}
In this case Laravel shows an error:
Class 'Options\FacebookOptionValidation' not found
But when I call my function like this, everything works fine:
use App\Validation\Options;
class ProfileUserEditController extends Controller {
public function updateUserOption()
{
Options\FacebookOptionValidation::validate();
}
}
As mentioned here, it's possible to call a class/function from a string. But in my case it's not possible - neither in the static or non-static variant.
Is that a 'laravel-thing'?
Try call with full namespace
class ProfileUserEditController extends Controller {
public function updateUserOption()
{
$class = 'App\Validation\Options\FacebookOptionValidation';
$class::validate();
}
}
With PHP7 you can even do this:
(App\Validation\Options\FacebookOptionValidation::class)::validate();
One line of code and without using a string
so i have a class here that have a function who requires another class to create an object.
I use namespace in both files, my question is can i get rid of this line here: include("class.php"); and instantiate class using namespace?
here is the file from where i call the other class:
namespace namespaceName;
class classLoader{
public function __construct() {
//not used
}
public function executeFunctionOutsideTheNamespace() {
include("class.php");
new classExtended("badass");
}
}
and the class by itself:
namespace namespaceName;
class classExtended extends classBase
{
public function __construct($action) {
echo $action;
}
}
I ask again, using Namespace there is no possibility to get rid of include() or require(), require_once() functions? to call directly new classExtended("badass"); ?
I'm new to PHP, and was trying to create an Abstract class with a mix of abstract and non-abstract methods, and then extend the class to implement the abstract methods. The following is portions of my two class files:
<?php
require_once 'Zend/Db/Table/Abstract.php';
abstract class ATableModel extends Zend_Db_Table_Abstract {
abstract static function mapValues($post);
abstract static function getTableName();
public static function newEntry($post) {
$db = Zend_Db_Table_Abstract::getDefaultAdapter();
$data = mapValues($post, true);
$db->insert(getTableName(), $data);
$id = $db->lastInsertId();
return $id;
}
public static function getEntry($id){
$db = Zend_Db_Table_Abstract::getDefaultAdapter();
$db->setFetchMode(Zend_Db::FETCH_OBJ);
return $db->fetchRow("
SELECT *
FROM ".getTableName()."
WHERE ID = '".(int)$id."'
"
);
}
public static function editEntry($id,$post) {
$db = Zend_Db_Table_Abstract::getDefaultAdapter();
$data = mapValues($post);
$db->update(getTableName(), $data, " ID = '".(int)$id."' ");
}
public static function deleteEntry($id) {
$db = Zend_Db_Table_Abstract::getDefaultAdapter();
$db->delete(getTableName()," ID = '".(int)$id."' ");
}
}
?>
The child class looks as follows:
<?php
require_once 'Zend/Db/Table/Abstract.php';
class Testing extends ATableModel {
public static function getTableName()
{
return 'TESTING';
}
public static function mapValues($post)
{
$data = array (
'test_description' => htmlentities($post['testDescription'])
);
return $data;
}
}
?>
Both files are located in the same directory relative to one another. However, when I try to run my application, I get the following error:
Fatal error: Class 'ATableModel' not found in /var/www/testApp/application/models/testing.php on line 20
My guess is that there's something wrong with either the order that I'm loading the files, or with where these files are located, relative to one another. However, I'm not sure how to proceed from here. Suggestions?
You're not including the file with your ATableModel definition.
<?php
// in your test
require_once 'Zend/Db/Table/Abstract.php'; // <- should be the file with ATableModel
In your child class you must include() or require() (or require_once()) the class file you are extending. I'm not familiar with Zend however, and if that framework is supposed to include all files in the same directory, don't know.
Try to add some code to make sure the file containing ATableModel is being included.