I have a base class A:
class A {
public static function a() {
...
}
public static function b() {
...
}
}
and an extended class B
class B extends A {
public static function a() {
...
}
public static function c() {
...
}
}
I would like to be able to call all the methods using B::
How would I call A::b, using B::?
You should be able to accomplish this as easily as:
class B extends A {
public static function a() {
parent::a();
}
}
See the docs
Related
I cant manage to call a static function (with a constant) from a extended class. Here is my code:
(1st file)
class A
{
function isXSet()
{
return X;
}
public static function setX()
{
define('X', 1);
}
}
(second file)
include('/*first file*/');
class B extends A
{
A::setX();
}
How can i manage to do that ?
Your code here
class B extends A
{
A::setX();
}
is a little off. You didn't put your call inside of a method.
class B extends A
{
public static function doSomething() {
A::setX();
}
}
This isn't actually doing anything by means of the parent/child relationship. In fact, after you define class A, the call to A::setX() can happen anywhere since it's public and static. This code is just as valid:
class A
{
function isXSet()
{
return X;
}
public static function setX()
{
define('X', 1);
}
}
class B { // No extending!
function isXSet() {
return A::isXSet();
}
}
What you're more likely looking for is parent instead:
class A
{
public function isXSet()
{
return X;
}
protected static function setX()
{
define('X', 1);
}
}
class B extends A
{
public static function doSomething() {
parent::setX();
var_dump( parent::isXSet() ); // int(1)
}
}
A big plus here is that extending classes can access protected methods and properties from the parent class. This means you could keep everyone else from being able to call A::setX() unless the callee was an instance of or child of A.
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.
How do you access a child method eg.?
class A
{
public function Start()
{
// Somehow call Run method on the B class that is inheriting this class
}
}
class B extends A
{
public function Run()
{
...
}
}
$b = new B();
$b->Start(); // Which then should call Run method
Class A should not try to call any methods that it itself does not define. This will work just fine for your scenario:
class A {
public function Start() {
$this->Run();
}
}
However, it will fail terribly should you actually do this:
$a = new A;
$a->Start();
What you're trying to do here sounds very much like a use case for abstract classes:
abstract class A {
public function Start() {
$this->Run();
}
abstract function Run();
}
class B extends A {
public function Run() {
...
}
}
The abstract declaration will precisely prevent you from shooting your own foot by trying to instantiate and Start A without extending and defining required methods.
If B is inherited from A then B will be like:
class B extends A
{
public function Start()
{
...
}
public function Run()
{
...
}
}
So as Run() and Start() are in the same class, we can call Run() in Start() directly.
public function Start()
{
Run();
}
How Can i call subclass method from superclass constructor?
Example subclass:
<?php
include('../classes/A.php');
class B extends A {
public function __construct()
{
parent::__construct($this->view);
}
public function view() {
//something
}
}
$b = new B;
?>
Example superclass:
<?php
abstract class A
{
private $callback;
public function __construct($callback)
{
$this->callback = $callback;
call_user_func($this->callback);
}
}
?>
What can I do to make it works?
Pass an array containing your object instance $this, and the method to call
(see Example #4 of the call_user_func man page on calling class methods)
class B extends A {
public function __construct()
{
parent::__construct(array($this, 'view'));
}
public function view() {
//something
}
}
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();