overloading conditionally in php - php

Might be I am missing something obvious.
I have a native class with set and get method.
class DBStorage extends NativeClass{
public function get($key);
public function set($key,value);
}
I would like to use that most of the time, but, if I turn on the system DEBUG flag.
I would like the set and get methods to be overloaded with the following:
IF DEBUG IS ON{
class DBStorage extends NativeClass{
public function get($key){
var_dump($key);
parent::get($key);
}
public function set($key,$value){
var_dump($key,$value);
parent::set($key,$value);
}
}
}
NativeClass is written in C. It is an extension (phpredis, but it is not relevant).
How would I accomplish this?
I am on the 5.3 branch of PHP.
just to make sure...if debug is off, DBStorage will be:
class DBStorage extends NativeClass{}
if debug is on, it will be:
class DBStorage extends NativeClass{
public function get($key){
var_dump($key);
parent::get($key);
}
public function set($key,$value){
var_dump($key,$value);
parent::set($key,$value);
}
}
I do try to avoid the cluttring of IFs (there are dozens of functions in the real class)
public function get($key) {
if (DEBUG) {
var_dump($key);
}
return parent::get($key);
}

You can't conditionally overload, but you can conditionally do something in the overloaded method:
class DBStorage extends NativeClass{
public function get($key) {
if (DEBUG) {
var_dump($key);
}
return parent::get($key);
}
}
If debug is off, it passes the arguments right through to the parent method and returns the parent's return value, as if nothing happened.

Your initial code won't compile, because of the if around the class construct. Why not just make available a debug member variable, and if true, echo output, or push into a log file?
class NativeClass
{
public $debug = false;
}
class DBStorage extends NativeClass
{
public function get($key)
{
if (true === $this->debug) {
error_log(sprintf('Key: %s', $key));
}
parent::get($key);
}
}
// Calling code
$dbo = new DBStorage();
$dbo->debug = true;
$dbo->doStuff();

something just came to me
class nodebug extends NativeClass{
static public function create(){
if(DEBUG) return new DebugNativeClass;
return new self;
}
}
class DebugNativeClass extends nodebug{
public function set($key,$value){
var_dump($key,$value);
parent::set($key,$value);
}
public function get($key){
var_dump($key);
return parent::set($key);
}
}

Related

non static method can't be called statically

I am so lost with how to get this working. All I want to do is to be able to call a function from another class and return the value.
in livewire component
use Livewire\Component;
use App\Actions\Broadcast\GetCurrentActiveTimeSlotAction;
class DisplayLiveBroadcastCard extends Component
{
public $timeSlot;
public function mount()
{
$this->refreshTest();
dd($this->timeSlot);
}
public function refreshTest()
{
$this->timeSlot = GetCurrentActiveTimeSlotAction::execute();
}
inside the GetCurrentActiveTimeslot class
class GetCurrentActiveTimeSlotAction
{
public $test;
public function __construct()
{
$this->test = 5;
}
public function execute()
{
$value = $this->test;
return $value;
}
}
Yes, I did rename it to static function execute() but that broke another thing where now I get an error trying this
static function execute()
{
$value = $this->test;
return $value;
}
Alternatively, I tried this approach as well, ut now it says I need to pass a variable into the refreshTest function. Which I understand, but anything I pass in there seems to break it.
public function mount()
{
$this->refreshTest();
dd($this->timeSlot);
}
public function refreshTest(GetCurrentActiveTimeSlotAction $getCurrentActiveTimeSlotAction)
{
$this->timeSlot = $getCurrentActiveTimeSlotAction->execute();
}
Looking for any advice on how I can just do a calculation in the GetCurrentActiveTimeSlotAction and return the value inside the livewire component.
Assuming you don't want to do the trivial thing (e.g. $this->timeSlot = (new GetCurrentActiveTimeSlotAction)->execute();) and want to do dependency injection instead (because that will make your code more testable) then you can inject objects in your mount method (source):
use Livewire\Component;
use App\Actions\Broadcast\GetCurrentActiveTimeSlotAction;
class DisplayLiveBroadcastCard extends Component
{
public $timeSlot;
private $activeTimeslotActionGetter;
public function mount(GetCurrentActiveTimeSlotAction $getter)
{
$this->activeTimeslotActionGetter = $getter;
$this->refreshTest();
dd($this->timeSlot);
}
public function refreshTest()
{
$this->timeSlot = $this->activeTimeslotActionGetter->execute();
}

Laravel Why I can access the method was not declared from Interface?

I have a problem like below:
I have an Interface name IBannerService
<?php
namespace App\Interfaces;
interface IBannerService
{
public function add($data);
public function list();
public function get($data);
public function delete($data);
}
and an instance name BannerService
class BannerService implements IBannerService
{
public function add($data)
{
return true;
}
public function list()
{
return true;
}
public function get($data)
{
return true;
}
public function delete($data)
{
return true;
}
public function test()
{
print_r("aaaa");
die();
}
}
finally I have a Controller name HomeController
class HomeController extends Controller
{
public function __construct(
IBannerService $bannerService
)
{
$this->bannerService = $bannerService;
}
public function index()
{
$listBanner = $this->bannerService->list();
$this->bannerService->test();
}
}
My configuration:
class DIServiceProvider extends ServiceProvider
{
$this->app->bind(
'App\Interfaces\IBannerService',
'App\Services\BannerService'
);
}
In app.php:
'providers'=>[
App\Providers\DIServiceProvider::class,
]
The code run well with $listBanner = true (just for testing).
The problem is:
Test Method was not declared in interface IBannerService but still go through and print out "aaa" the die.
Did I do something wrong?
Please suggest me, thank you!
That's perfectly normal functionality.
In the Laravel container you defined that when you ask for a IBannerService object, you want to get a BannerService class. And that is what you got. BannerService is an implementation of IBannerService, so no problem for the typehint.
A class is not limited to the functions defined by its interface so you can add as many other functions as you like. I wouldn't recommend it though, things like smart IDE's and phpstan would give you errors or warnings because to them, the variable is an implementation of IBannerService and this does not have a test() function.
If you really want to use more functions I would even recommend to use BannerService as the typehint. This way, static code analysis will still work.

PHP OOP classes and functions

I am not sure how to name this, but here it goes. Lets suppose i have the following
class A {
public function aa() {
$this->bb();
}
public function bb() {
}
}
class B extends a {
}
class C {
__construct(B $service) {
$this->service = $service;
}
public function aa() {
$this->service->aa();
}
}
My call in code will be
$C = new C(new B());
$C->aa();
So this will basically execute A:aa() which is what i want. As you can see, in A::aa() AA::bb() is called.
What I need. When AA::bb() is called i want to execute some code defined in class C, but I am not allowed to change the A class. I can only change the B class or the C class.
My idea was to add a listener in the B class and overwrite the bb() function like this
class B extends a {
public $listener;
bb() {
parent::bb();
$this->listener();
}
}
class C {
__construct(B $service) {
$this->service = $service;
}
public function aa() {
$this->service->listener = function() { }
$this->service->aa();
}
}
But I don't like this idea a lot, doesn't look like a good one. What are my options here?
Again, I CANNOT change the A class and i can only call the C class.
PHP version is 5.3
You have two options. Extend or decorate.
First one would be kinda what you have already written, though, I would not use public visibility for the listener:
class Foo extends A {
private $listener;
public function setListener(callable $func) {
$this->listener = $func;
}
public function bb() {
call_user_func($this->listener);
return parent:bb();
}
}
In the example I passed the listener via setter injection, but you can also use constructor injection and pass the $listened in the overloaded __construct() method. When you extend a class, the "interface restriction" does not aply to the constructor's signature.
The other approach is to use a decorator:
class Foo {
private $target;
public function __construct(A $target) {
$this->target = $target;
}
public function bb($callback) {
$callback();
return $this->target->bb();
}
public function __call($method, $arguments) {
return call_user_func_array(
array( $this->target, $method ),
$arguments
);
}
}
The second approach would let you alter the interface.
Which option you pick depend on the exact functionality you actually need to implement. The decorator is a solution for, when you need drastic change in the objects behavior - for example, it is really good for adding access control.
I understand that you want to execute code in C after code in A completes. You cannot change A.
As written, C::aa calls A::aa, which calls A::bb and the stack unwinds. Why not just do the work in C::aa after the service call finishes?
class C {
public function aa() {
$this->service->aa();
// whatever you want to do
}
}
If, on the other hand, you need to call code after A::aa is called but before A::bb is called then the example you posted would suffice with clarity:
class B extends a {
public $listener;
public function bb() {
call_user_func($this->listener);
parent::bb();
}
}
Note the use of call_user_func, which is necessary for PHP 5.3 to call an anonymous function stored in a member variable.

Trait with same method as Class , one Static

trait TestTrait
{
public function matches()
{
}
}
class TestClassOne
{
public static function matches()
{
}
}
class TestClassTwo extends TestClassOne
{
use TestTrait {
matches as alternativeMatches;
}
public function run()
{
$this->alternativeMatches();
}
}
When calling the method run as seen above it will return the error :Fatal error: Cannot make static method matches non static
The classes are not written by me and the trait I have used in multiple other locations. A hell if I have to rewrite the trait .. or have to make a new one , just to make it work with this class.
Is there a way to make it work ?
Cheers
Well I made it work with a bit of a hack ... but I do not like it...
Someone has a nicer way of doing this ?
class TestClassTwo extends TestClassOne
{
use TestTrait {
matches as alternativeMatches;
}
public function run()
{
$this->alternativeMatches();
}
public static function matches()
{
parent::matches();
}
}

Why use a return when calling parent?

I am learning OO PHP and I was experimenting with using a parent::method in a child class. I noticed i had to use an "extra" return for the output of the parent method to show up. Could someone explain me why this is?
This is the code I used and in the code I made a comment.
class ShopProduct {
public $productnumber;
public function __construct($productnumber) {
$this->productnumber = $productnumber;
}
public function getSummary(){
return $this->productnumber;
}
}
class BookProduct extends ShopProduct {
public function __construct($productnumber) {
parent::__construct($productnumber);
}
public function getSummary() {
return parent::getSummary(); // if i dont use return it doesnt work? why is that?
// parent::getSummary(); is not enough it seems.
}
}
$product = new BookProduct(11111);
echo $product->getSummary();
?>
public function getSummary() {
return parent::getSummary(); // if i dont use return it doesnt work? why is that?
// parent::getSummary(); is not enough it seems.
}
Replace parent::getSummary() with any other function or method call:
public function getSummary() {
foo();
}
Of course you wouldn't expect getSummary to return anything in this case, right? Just because the method you're calling is parent::... doesn't change anything about this behaviour. It does not return automagically, because you may want to do something like this:
public function getSummary() {
$summary = parent::getSummary();
return "Book: $summary";
}
BTW, if the only thing your method does is call its parent, you can leave out the entire method. In other words, this:
class BookProduct extends ShopProduct {
public function __construct($productnumber) {
parent::__construct($productnumber);
}
public function getSummary() {
return parent::getSummary();
}
}
is exactly the same as this:
class BookProduct extends ShopProduct { }

Categories