I wan't to execute constructor in my trait (or another method while trait is used). Is it possible?
trait test{
public function __construct()
{
echo 'test';
}
}
class myClass{
use test;
public function __construct(){
echo 'myClass';
}
}
new myClass();
Try it like this (test):
trait test{
public function __construct()
{
echo 'test';
}
}
class myClass{
use test {
test::__construct as private __tConstruct;
}
public function __construct(){
$this->__tConstruct();
}
}
new myClass();
Related
How can I stub a method of an object that is hardcoded in app? In rspec there is a method allow_any_instance_of.
I can't reverse the dependency, the initialisation of the object should still be hardcoded.
So, I have ClassA
namespace App
class ClassA
{
public function doSomething(){
// more code
return($sth);
}
}
It is used in ClassB
namespace App
class ClassB
{
protected $instanceOfA;
public function __construct(){
$this->instnaceOfA = new ClassA();
}
public function methodToTest(){
$result = $this->instanceOfA->doSomething()
// more code
}
}
I think this is what you're looking for? a plugable interface? if you change the classB to ClassA on line 33 it will switch to the other class.
Interface TheInterface
{
public function doSomething();
}
class ClassA implements TheInterface
{
public function doSomething(){
echo __METHOD__;
}
}
class ClassB implements TheInterface
{
public function doSomething(){
echo __METHOD__;
}
}
class ClassProcess
{
protected $instance;
public function __construct(TheInterface $class){
$this->instance = $class;
}
public function methodToTest(){
$this->instance->doSomething();
}
}
$process = new ClassProcess(new ClassB());
$process->methodToTest();
I'm a bit rusty with php, I want to know how I can call the function login available in class2, inside class1. This is the example:
<?php
require_once("property2.php");
class Class1
{
public function __construct()
{
$cls2 = new Class2()
}
public function method1()
{
$cls2->login() //cl2 is undefined
}
} ..
//this is the function
...
class Class2
{
public function __construct()
{
}
//This is the function to call
public function login()
{
//Some stuff
}
} ...
Now PHPSTORM say that the variable cls2 is undefined. What I did wrong?
When you are setting your variable youre not setting it as a class property. Define a private variable inside your class, and "set it and get it" using the $this keyword.
class Class1 {
private $cls2;
public function __construct() {
$this->cls2 = new Class2();
}
public function method1() {
$this->cls2->login();
}
}
Another way to achieve this is to use Inheritance, where one class is considered a "parent" class. You would achieve this by using extends
class Class1 {
public function __construct() {
//Some stuff
}
public function login() {
//Some stuff
}
}
class Class2 extends Class1 {
public function __construct() {
parent::__construct();
}
public function method1() {
$this->login();
}
}
class Class1
{
public function __construct()
{
$cls2 = new Class2();
}
public function method1()
{
$cls2->login() //cl2 is undefined
}
}
When you create Class1 and call $cls2 = new Class2();, $cls2 exists only locally. You have to make it a class property:
class Class1
{
public $cls2;
public function __construct()
{
$this->cls2 = new Class2();
}
public function method1()
{
$this->cls2->login();
}
}
And then you'll be able to access it using $this keyword.
Also please watch for semicolons.
class Hello {
public function hi() {
echo "Hello, hi!\n";
}
}
class ParentClass {
public $obj;
public function __construct() {
$this->obj = new Hello;
}
}
class Test extends ParentClass {
public function __construct() {
$this->obj->hi();
}
}
$temp = new Test;
The error message I get is "Call to a member function hi() on a non-object". $obj should be referencing to an instance of the class "Hello", but it obviously is not - what am I doing wrong?
You are defining __construct() in your Test class but not calling the parent constructor. If you want the parent constructor to execute, you need to explicitly specify so. Add a call to ParentClass constructor in in Test class constructor.
class Test extends ParentClass {
public function __construct() {
parent::__construct();
$this->obj->hi();
}
}
Also as #Tasos Bitsios pointed in his comment you also need to update your ParentClass constructor as follows:
class ParentClass {
public $obj;
public function __construct() {
$this->obj = new Hello; // Use $this->obj and not just $obj.
}
}
You need call to parent constructor:
class Test extends ParentClass {
public function __construct() {
parent::__construct();
$this->obj->hi();
}
}
I have a trick question regarding PHP calling a function from parent in child class.
We have 3 scenarios, and I want pros and cons.
<?php
class test{
private $var ;
public function __construct(){
$this->var = 'Hello world';
}
public function output(){
echo $var.'<br>';
}
}
//scenario 1
class test1 extends test{
public function __construct(){
parent::__construct();
}
public function say(){
parent::output();
}
}
//scenario 2
class test2 extends test{
public function __construct(){
test::__construct();
}
public function say(){
test::output();
}
}
//scenario 3
class test3 extends test{
private $handle ;
public function __construct(){
$this->handle = new test();
}
public function say(){
$this->handle->output();
}
}
//finally I can call any 3 cases by one of the below codes
$test1 = new test1();
$test1->say();
//or
$test2 = new test2();
$test2->say();
//or
$test3 = new test3();
$test3->say();
?>
Is there a best practice or is there any of the 3 scenarios better than other?
Thank you in advance.
1) Is correct
2) Is incorrect call the method like a static method.
3) It does not have any sense extend and create in the constructor.
1)
This one is correct, as its calling the parent from its methods.
class test1 extends test{
public function __construct(){
parent::__construct();
}
public function say(){
parent::output();
}
}
2)
The inheritance in here is unnecessary.
If you choose this implementation, you must change both output and construct method to static.
//scenario 2
class test2 extends test{
public function __construct(){
test::__construct();
}
public function say(){
test::output();
}
}
3)
The inheritance from here is also unnecessary.
Note here you are using "component over inheritance" pattern which is a good practice as it provides more flexibility, but you must delete the "extends test".
//scenario 3
class test3 extends test{
private $handle ;
public function __construct(){
$this->handle = new test();
}
public function say(){
$this->handle->output();
}
}
I'm trying to accomplish this without requiring a function on the child class... is this possible? I have a feeling it's not, but I really want to be sure...
<?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(); //returns B
?>
Use get_called_class() instead of __CLASS__. You'll also be able to replace static with self as the function will resolve the class through late binding for you:
class A {
public static function who() {
echo get_called_class();
}
public static function test() {
self::who();
}
}
class B extends A {}
B::test();