How to delete parent static methods in extended class - php

Here is my model
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Associate extends Model
{
// some code
}
In controller I use this model similar this
<?php
namespace App\Http\Controllers;
use App\Models\Associate;
use Illuminate\Http\Request;
class AssociatesController extends Controller
{
protected $associate;
public function __construct(Associate $associate)
{
$this->associate = $associate;
}
public function edit(Request $request, $id)
{
$associate = $this->associate->with('some-relation')->find($id);
// other part of code
}
}
When i wont to testing in controller edit method using phpunit I cant mock with method because it is static method of Illuminate\Database\Eloquent\Model.
My question there is way to delete some method of parent class??

From Laravels documentation
static Builder|Model with(array|string $relations)
Being querying a model with eager loading.
From Php docs
<?php
class A {
public static function who() {
echo __CLASS__;
}
public static function test() {
static::who(); // Here comes Late Static Bindings
}
}
class B extends A {
public static function who() {
echo __CLASS__;
}
}
B::test();
?>
The above example will output:
B

Related

How to correctly extend and use other interfaces?

I'm trying to make use of a base interface for all my other interfaces as follows:
Base interface
<?php
namespace App\Repositories\Data;
interface IDataRepository
{
public function getAll();
public function getById($id);
public function create($model);
public function update($model);
public function delete($id);
}
Implemented base interface
<?php namespace App\Repositories\Data;
use Illuminate\Database\Eloquent\Model;
class DataRepository implements IDataRepository
{
// model property on class instances
protected $model;
// Constructor to bind model to repo
public function __construct(Model $model)
{
$this->model = $model;
}
// Get all instances of model
public function getAll()
{
return $this->model->all();
}
// create a new record in the database
public function create($model)
{
return $this->model->create($model);
}
// update record in the database
public function update($model)
{
$record = $this->find($model.id);
return $record->update($model);
}
// remove record from the database
public function delete($id)
{
return $this->model->destroy($id);
}
// show the record with the given id
public function getById($id)
{
return $this->model-findOrFail($id);
}
}
The interface where i'm trying to make use of the base interface
<?php
namespace App\Repositories;
use App\Repositories\Data\IDataRepository;
interface ITestRepository extends IDataRepository
{
}
implementation
<?php namespace App\Repositories;
use App\Library\Classes\Test;
use Illuminate\Database\Eloquent\Model;
class TestRepository implements ITestRepository
{
}
In my controller i'm trying to just call test repository so i can use all the base repository functions:
class TestController extends Controller
{
protected $testRepository;
public function __construct(Test $test)
{
$this->testRepository = new TestRepository($test);
}
public function index()
{
$data['testData'] = $this->testRepository->getAll();
return view('test', $data);
}
}
But i get the following error:
Class App\Repositories\TestRepository contains 5 abstract methods and
must therefore be declared abstract or implement the remaining methods
My application works fine if i only make use of my base interface and pass through a model. What would be the correct way to share functions from my base interface across all my other interfaces, so as to prevent code duplication? I appreciate any help.
I think that a Trait which will contains all methods of your interface declaration is the best choice. Something like (not sure about logic):
namespace App\Repositories;
trait TDataRepository
{
// model property on class instances
protected $model;
// Constructor to bind model to repo
public function __construct(Model $model)
{
$this->model = $model;
}
// Get all instances of model
public function getAll()
{
return $this->model->all();
}
// create a new record in the database
public function create($model)
{
return $this->model->create($model);
}
// update record in the database
public function update($model)
{
$record = $this->find($model.id);
return $record->update($model);
}
// remove record from the database
public function delete($id)
{
return $this->model->destroy($id);
}
// show the record with the given id
public function getById($id)
{
return $this->model-findOrFail($id);
}
}
And then just use it for classes with base interface:
namespace App\Repositories;
use App\Library\Classes\Test;
use Illuminate\Database\Eloquent\Model;
class TestRepository implements ITestRepository
{
use TDataRepository;
}
Also there are some other options:
abstract class with methods for base interface but it not so flexible like trait,
composition but you should change base idea and create a new entity for composition.
<?php
namespace App\Repositories;
use App\Interfaces\ITestRepository;
class TestRepository implements ITestRepository
{
public function getAll()
{
// TODO: Implement getAll() method.
}
public function getById($id)
{
// TODO: Implement getById() method.
}
public function create($model)
{
// TODO: Implement create() method.
}
public function update($model)
{
// TODO: Implement update() method.
}
public function delete($id)
{
// TODO: Implement delete() method.
}
}
Class must be declared abstract or implement methods 'getAll', 'getById', 'update', 'create', 'delete'
So All the method is by default abstract method in interface and you have to define all method in this class.
The class TestRepository should not implement any interface, but extend DataRepository:
<?php namespace App\Repositories;
use App\Repositories\Data\DataRepository;
class TestRepository extends DataRepository
{
}
DataRepository contains already the implementation of the interface IDataRepository. When you create a class implementing ITestRepository you will have to define the implementation of all the methods in the interface (which are the same as the base interface, in your case).

PHP/Laravel parent class variable is null when calling from different controllers

Parent Class:
<?php
namespace App\Services;
class RequestVariables {
protected static $keys_tour;
public static function init() {
self::$keys_tour = array_flip(['tour_type', 'city_from']);
}
}
Child Class:
<?php
namespace App\Services;
class PreviousVersions extends RequestVariables {
public static function createVersion ($tour) {
dd(parent::$keys_tour);
}
}
When I call PreviousVersions::createVersion() from 1st controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Services\PreviousVersions;
use App\Tour;
class Tours2Controller extends Controller
{
public static function PreProcess($tour)
{
PreviousVersions::createVersion($tour);
}
}
it outputs what's expected:
array:2 [
"tour_type" => 0
"city_from" => 1 ]
but when I execute the same function in another controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Tour;
use App\Services\PreviousVersions;
class BookingController extends Controller {
public function booking($tour)
{
PreviousVersions::createVersion($tour);
}
}
it outputs 'null'
I can't see what's different between my controllers causing different results when calling the same method. Can somebody tell me why it outputs 'null' in the 2nd case?
If you need more information, please ask.
The $keys_tour property is being set inside the init() method of the RequestVariables class.
You can solve it by calling RequestVariables::init() inside the createVersions() method:
public static function createVersion ($tour)
{
RequestVariables::init();
}
Or using the parent keyword:
public static function createVersion ($tour)
{
parent::init();
}

laravel parent constructor does not called child constructor

I am using laravel 5.2,
I have created admin controller and added logic to check admin role in constructor
namespace App\Http\Controllers;
use Sentinel;
class AdminController extends Controller
{
public function __construct()
{
if(Sentinel::check())
{
if(!Sentinel::inRole('admin'))
{
return redirect("login");
}
}
else
{
return redirect("login");
}
}
}
and I extends this controller on some admin controller
namespace App\Http\Controllers;
use Request;
use App\Http\Controllers\AdminController;
use App\Http\Requests;
use Sentinel;
use App\User;
use DB;
class UserController extends AdminController
{
function __construct()
{
parent::__construct();
}
}
When I call user controller admin constructor is called but return function is not working properly, if I add die; before return it get die, but after return notthing is affected.
so it doesn't return redirect function properly.
The ugly workaround would be to pass a boolean param to Papa indicating that you do not wish to parse the code contained in it's constructor. i.e:
// main class that everything inherits
class Grandpa extends Controller
{
public function __construct()
{
}
}
class Papa extends Grandpa
{
public function __construct($bypass = false)
{
// only perform actions inside if not bypassing
if (!$bypass) {
}
// call Grandpa's constructor
parent::__construct();
}
}
class Kiddo extends Papa
{
public function __construct()
{
$bypassPapa = true;
parent::__construct($bypassPapa);
}
}

PHP Laravel: Trait not found

I have some trouble with namespace and use.
I get this error: "Trait 'Billing\BillingInterface' not found"
These are the files in my Laravel application:
Billing.php
namespace Billing\BillingInterface;
interface BillingInterface
{
public function charge($data);
public function subscribe($data);
public function cancel($data);
public function resume($data);
}
PaymentController.php
use Billing\BillingInterface;
class PaymentsController extends BaseController
{
use BillingInterface;
public function __construct(BillingPlatform $BillingProvider)
{
$this->BillingProvider = $BillingProvider;
}
}
How to i use use and namespace properly?
BillingInterface is an interface not a trait. Thus it can't find the non existent trait
Also you have an interface called BillingInterface in a namespace called Billing\BillingInterface, the fully qualified name of the interface is: \Billing\BillingInterface\BillingInterface
Perhaps you mean
use Billing\BillingInterface\BillingInterface;
// I am not sure what namespace BillingPlatform is in,
// just assuming it's in Billing.
use Billing\BillingPlatform;
class PaymentsController extends BaseController implements BillingInterface
{
public function __construct(BillingPlatform $BillingProvider)
{
$this->BillingProvider = $BillingProvider;
}
// Implement BillingInterface methods
}
Or to use it as a trait.
namespace Billing;
trait BillingTrait
{
public function charge($data) { /* ... */ }
public function subscribe($data) { /* ... */ }
public function cancel($data) { /* ... */ }
public function resume($data) { /* ... */ }
}
Again the modified PaymentsController, but with fully qualifies names.
class PaymentsController extends BaseController
{
// use the fully qualified name
use \Billing\BillingTrait;
// I am not sure what namespace BillingPlatform is in,
// just assuming it's in billing.
public function __construct(
\Billing\BillingPlatform $BillingProvider
) {
$this->BillingProvider = $BillingProvider;
}
}

Yii controller inheritance ( in submodule )

My question is, how can I inherit Controllers action in YII, like:
class MainController extends FController
{
public function ActionIndex()
{
$this->render("index"); //the view;
}
}
--------------------------------------------
class SecondController extends FController
{
public function ActiondIndex()
{
MainController::ActionIndex();
}
}
Actually in my case the SecondController is the DefaultController of a sub-module. I want to make single code based webpage.
Since PHP does not support multiple inheritance, you may access the base class via the parent keyword.
class MainController extends FController
{
public function ActionIndex()
{
$this->render("index"); //the view;
}
}
class SecondController extends FController
{
public function ActionIndex() // Note: you mistyped the name of this action in your example
{
parent::ActionIndex();
}
}
Although you cannot reach the grandparent's ActionIndex method directly, you have to create a workaround for that.

Categories