access parent method from child object with attributes in php? - php

I have the following PHP classes:
class a {
public function vw($xc) {
return $xc;
}
}
class b extends a {
public function wv() {
echo vw() . 'from b via wv';
}
}
$d = new a;
echo $d->vw('this is a');
$c = new b;
echo $c->vw('this is a from b via a');
$c->wv();
The output I am getting is:
this is a
Why am I not getting the outputs of echo $c->vw('this is a from b via a'); and c->wv(); ?

You can access a parent's method via parent::, e.g. parent::vw(). But the method vw of class a expects a parameter, so this code snippet won't work at all. But you should get the idea of using the parent keyword.
class a {
public function vw($xc) {
return $xc;
}
}
class b extends a {
public function wv() {
echo parent::vw() . 'from b via wv';
}
}
$d = new a;
echo $d->vw('this is a');
$c = new b;
echo $c->vw('this is a from b via a');
$c->wv();
http://php.net/manual/en/keyword.parent.php
https://3v4l.org/0MkQI

In the class "b", you should write the function vw as:
public function wv(){
echo $this->vw() . "from b via wv\n";
}
Besides, in your last line the call of $c->wv() lacks a parameter:
$c->wv("I'm a missing parameter");

Try this
class a {
public tt;
public function vw($xc){
$this->tt = $xc;
return $this->tt;
}
}
class b extends a
{
public function(){
return $this->tt. 'from b via wv'
}
}

Related

Does this smell if I cant decide which descendant should I use?

abstract class X
{
private $v;
protected function setV($v)
{
$this->v = $v;
}
public function getV()
{
return $v;
}
}
class A extends X
{
public function doIt()
{
parent::setV(1);
}
}
class B extends X
{
public function doIt()
{
parent::setV(2);
}
}
$a = new A();
$a->doIt();
$b = new A();
$b->doIt();
but if I want to use getV(), I can both call
$a->getV() and $b->getV()
which sounds silly. Which one to use? To be honest, I would like to see something like that:
X::getV();
which is not possible, an instance must be exists/
It depends on "what do you want". Firstable, it's possible to use X::getV() method, but you need to make v member and getV method static, as shown below.
<?php
abstract class X
{
private static $v;
protected static function setV($v)
{
self::$v = $v;
}
public static function getV()
{
return self::$v;
}
}
class A extends X
{
public function doIt()
{
self::setV(2);
}
}
class B extends X
{
public function doIt()
{
self::setV(1);
}
}
$a = new A();
$a->doIt();
echo X::getV();
// prints 2
// but be aware, that ANY instance of X children class will change the same X::$v value
$b = new B();
$b->doIt();
echo X::getV();
// prints 1
Static members (like X::$v) are stored only once, they are "binded" to the class, not to the instance of this class.
<?php
class Foo
{
public static $v = 5;
}
$instance1 = new Foo();
$instance2 = new Foo();
echo Foo::$v;
echo $instance1::$v;
echo $instance2::$v;
// prints 5, 5, 5
$instance1::$v = 10;
echo Foo::$v;
echo $instance1::$v;
echo $instance2::$v;
// prints 10, 10, 10

Php, inheritance, late static binding, unexpected calling chain

consider this code:
class C
{
public function get()
{
echo 'C';
static::get();
}
public function save()
{
self::get();
}
}
class B extends C
{
public function get()
{
echo 'B';
static::get();
}
}
class A extends B
{
public function get()
{
echo 'A';
}
}
$x = new A();
$x->save();
it echoes CA while I was expected CBA
To get this to work the way you want, reverse the logic - get your save to call the static::get() so it will start at the top of the inheritence tree; and use calls to parent::get() in each inherited class in the tree (except the base level) before echoing its own output
class C
{
public function get()
{
echo 'C';
}
public function save()
{
static::get();
}
}
class B extends C
{
public function get()
{
parent::get();
echo 'B';
}
}
class A extends B
{
public function get()
{
parent::get();
echo 'A';
}
}
$x = new A();
$x->save();
Demo

How can I create a new object based on the current?

I have a base class and multiple classes that extends from the base class.
class B {}
class C extends B {}
class D extends B {}
How can I create a C or D inside the method dynamically from B? what's the best way?
For example I tried:
class B {
function hello() { echo "hello"; }
function createObject()
{
$temp = new self();
$temp->hello();
}
}
$t = new C();
$t->createObject();
You're right! But you must return your new object like:
class B {
function hello() { echo "hello"; }
function createObject()
{
$temp = new self();
$temp->hello();
return $temp; // <--- here
}
}
$t = new C();
$tNew = $t->createObject();

PHP - Which class invoked my static method?

I need to know what kind invokes a static method, without sending as parameter
class foo
{
public static function test($clase)
{
echo "Class invoke:" . FUNCTION();
}
}
class A { public function x { foo::test(); } }
class B { public function y { foo::test(); } }
class C { public function z { foo::test(); } }
You can use late static bindings and get_called_class() (PHP >= 5.3) if you make all of your classes extend foo, like this:
class foo
{
public static function test($clase)
{
echo "Class invoke:" . get_called_class();
}
}
class A extends foo { public function x() { self::test(''); } }
class B extends foo { public function y() { self::test(''); } }
class C extends foo { public function z() { self::test(''); } }
With these objects:
$a = new A; $a->x();
$b = new B; $b->y();
$c = new C; $c->z();
You'll get as output:
Class invoke:A
Class invoke:B
Class invoke:C

How to access a variable that inherits to a php class that we called?

we have two class A & B:
class A{
var $settings;
function getinfo(){
$settings['domain']="mydomain";
$settings['pass']="1234";
return $settings;
}
}
class B extends A{
$ads = A::getinfo();
function makeurl(){
return "template/".$ads['domain'];
}
}
now i have an instance of B in my page, but i need "pass" , maybe some code like this:
$theme=new B();
$mypass = $theme->A->getinfo;
echo $mypass[$pass];
I know this code is full of faults , but i could not write a better one. is there any solution to access to password without making an instance of A?
Yes. It is as simple as this:
$theme = new B();
$mypass = $theme->getinfo();
echo $mypass['pass'];
You can also improve your classes a bit:
class A
{
var $settings;
function getinfo()
{
$this->settings['domain'] = "mydomain";
$this->settings['pass'] = "1234";
return $this->settings;
}
}
class B extends A
{
function makeurl()
{
$this->getinfo();
return 'template/' . $this->settings['domain'];
}
}
Why not call the settings variable in A from the B instance since B is a subclass of A?
Try this code:
<?php
class A
{
var $settings;
function getinfo()
{
$settings['domain'] = "mydomain";
$settings['pass'] = "1234";
return $settings;
}
}
class B extends A
{
function makeurl()
{
$ads = $this->getinfo();
return "template/" . $ads['domain'];
}
}
$theme=new B();
$mypass = $theme->getinfo();
echo $mypass['pass'];
What about making settings a public static variable in A? By making it a class variable you won't need an instance of A.
class A {
public static $settings;
// getter and setter methods here
}
// code elsewhere
echo A::$settings['pass'];
Also because your class B extends A it inherits the methods and properties, so you could call
$theme = new B();
$mySettings = $theme->GetInfo();
if B extends A, all protected and public members of A are inherited into B, so you can access them directly.
class A {
protected $foo;
public function __construct() { $this->foo = 1; }
}
class B extends A {
public function bar() {
echo $this->foo;
}
}
$b = B();
$b->bar();
If I understand you correctly, you're pretty close:
$theme=new B();
$settings = $theme->getinfo();
$mypass = $settings['pass'];
echo $mypass;

Categories