Get function name of child class in parent class - php

I would like to have a base class with basic properties and functions, so I dont have to define them in all child classes.
I use php 5.3.3.
Is this impossible ?
class A {
private $debug;
private $var;
protected function setVar($str) {
$this->debug = 'Set by function `'. MAGIC_HERE .'` in class `'. get_called_class() .'`.';
$this->var = $str;
return true;
}
protected function getVar() {
return $this->var;
}
protected function getDebug() {
return $this->debug;
}
}
class B extends A {
public function __construct() {
$this->doSomething();
}
public function doSomething() {
$this->setVar('my string');
}
}
$myobj = new B();
$myobj->getDebug();
// expected output "Set by function `doSomething` in class `B`."

<?php
class A {
private $debug;
private $var;
protected function setVar($str) {
$this->debug = 'Set by function `'. MAGIC_HERE .'` in class `'. get_called_class() .'`.';
$this->var = $str;
return true;
}
protected function getVar() {
return $this->var;
}
// Notice the public here, instead of protected //
public function getDebug() {
return $this->debug;
}
}
class B extends A {
public function __construct() {
$this->doSomething();
}
public function doSomething() {
$this->setVar('my string');
}
}
$myobj = new B();
echo $myobj->getDebug();
// expected output "Set by function `doSomething` in class `B`."
You had just two small issues. A::getDebug needed to be public to be accessible from the outside and you forgot to output the return of A::getDebug.

See the debug_backtrace function. Note this function is expensive, so you should disable those debug features in production.

Is this no good for you?
I'm not running 5.3 locally, so I had to switch out get_called_class() but you could still use it. Should have made that clear, sorry.
class A {
private $debug;
private $var;
protected function setVar($str, $class) {
$this->debug = 'Set by function `` in class `'. $class .'`.';
$this->var = $str;
return true;
}
protected function getVar() {
return $this->var;
}
public function getDebug() {
return $this->debug;
}
}
class B extends A {
public function __construct() {
$this->doSomething();
}
public function doSomething() {
$this->setVar('my string', __CLASS__);
}
}
$myobj = new B();
echo $myobj->getDebug();

Related

Creating dynamic class this pointer

I am noob in PHP because I am mostly do .NET/Java. In code base I am working, I have,
class SomeOtherBaseClass{
public $prop2;
public function __construct(string $prop3)
{
$this->prop2 = $prop3;
}
public function __toString()
{
return $this->prop2 . ' '. $this->prop2;
}
}
class SomeClass
{
public function __toString()
{
return $this->prop1 . ' '. $this->prop1;
}
public $prop1;
public function someMethod() : SomeOtherBaseClass
{
return $this->createClass();
}
public function __construct()
{
$this->prop1 = 'foo';
}
private function createClass(
): SomeOtherBaseClass {
return new class(
$this->prop1
) extends SomeOtherBaseClass {
};
}
}
$class = new SomeClass();
echo $class;
echo $class->someMethod();
Why I am getting error that prop1 not found. Clearly createClass function is part of SomeClass which have prop1. Why I cannot access prop1 inside createClass?
It's because $prop1 has no value or meaning.
You can add a __construct() function to resolve your issue:
public function __construct()
{
$this->prop1 = 'foo';
}
now when you call this class (e.g. $foo = new SomeClass();):
$prop1 has a value of foo which can be used in your functions:
public function echoProp()
{
echo $this->prop1; # will output foo
}
Note: This is just an explanation answer - not a copy/paste solution - but the principles are all here for you to use in your code.
Let me know if this wasn't what you were looking for :)
Edit:
if prop1 exists in SomeOtherClass, when you construct you can do
public function __construct()
{
$this->class = new SomeClass();
$this->prop1 = $this->class->prop1;
}

object used as property guess the class of his owner

I was wondering if it is possible for an object to guess the class of his "owner" if the "owner" have a property of the object class.
Let me explain what I mean by a small example
class A {
public function Magic(){/*return owner's class*/}
}
class B {
private $foo;
public function __construct() {
$this->foo=new A();
}
public function getFoo(){
return $this->foo;
}
}
class C {
private $bar;
public function __construct() {
$this->bar=new A();
}
public function getBar(){
return $this->bar;
}
}
$b= new B();
$c= new C();
print($b->getFoo()->Magic()); // would print B
print($c->getBar()->Magic()); // would print C
I don't know if I am dreaming or if it's possible...
How would you do it if not possible?
You need to inject to A his owner. So, the Magic method is less "magical", but the dependency is more clear.
class A
{
private $owner;
public function __construct($owner)
{
$this->owner = $owner;
}
public function Magic() { return get_class($this->owner); }
}
class B
{
private $foo;
public function __construct()
{
$this->foo = new A($this);
}
public function getFoo()
{
return $this->foo;
}
}
class C
{
private $bar;
public function __construct()
{
$this->bar = new A($this);
}
public function getBar()
{
return $this->bar;
}
}
$b= new B();
$c= new C();
print($b->getFoo()->Magic()); // would print B
print($c->getBar()->Magic()); // would print C

passing variables from a protected function to a public function inside the same class in php

I have a class and two functions inside it as follows:
class MyClassName
{
protected function myFunction1()
{
// some code here
return $something;
}
public function myFunction2()
{
// some code here
return $somethingElse;
}
}
What I need to do is define a variable in myFunction1() and then use it in myFunction2(). What is the best practice to do that?
class MyClassName
{
public $var = 0;
protected function myFunction1()
{
// some code here
$this->var = ...;
return $something;
}
public function myFunction2()
{
// some code here
echo $this->var;
return $somethingElse;
}
}
Actually vars should be defined out of the function and then set a value. Then can be modified over all the script, by doing this->var
Make it a class property
class MyClassName
{
private $property;
public function __construct() {
$this->myFunction1();
}
protected function myFunction1()
{
// some code here
$this->property = 'an apple';
}
public function myFunction2()
{
// some code here
return $this->property;
}
}
Now test it:
$my_class = new MyClassName();
$something = $my_class->myFunction2();
echo $something;

Php Class - How can I make a Class know parent value

<?php
class FirstClass{
public static $second;
public static $result = 'not this =/';
public function __construct(){
$this->result = 'ok';
$this->second = new SecondClass();
}
public function show(){
echo $this->second->value;
}
}
class SecondClass extends FirstClass{
public $value;
public function __construct(){
$this->value = parent::$result; //Make it get "ok" here
}
}
$temp = new FirstClass();
$temp->show(); //It will show: "not this =/"
?>
How can I make it to print "ok"?
I mean, the SecondClass should know what FirstClass set as result, see?
Replace $this->result = 'ok'; with self::$result = 'ok'; in FirstClass constructor.
Btw, the code is terrible. You're mixing static and instance variables, and extend classes but don't use benefits extension provides.
you need to reference the static as self::$result in the first class.
Below should do what you want...
<?php
class FirstClass{
public static $second;
public static $result = 'not this =/';
public function __construct(){
self::$result = 'ok';
$this->second = new SecondClass();
}
public function show(){
echo $this->second->value;
}
}
class SecondClass extends FirstClass{
public $value;
public function __construct(){
$this->value = parent::$result; //Make it get "ok" here
}
}
$temp = new FirstClass();
$temp->show(); //It will show: "not this =/"
?>

extending static methods in php

class Foo {
public static function foobar() {
self::whereami();
}
protected static function whereami() {
echo 'foo';
}
}
class Bar extends Foo {
protected static function whereami() {
echo 'bar';
}
}
Foo::foobar();
Bar::foobar();
expected result foobar actual result foofoo
to make matters worse, the server is restricted to php 5.2
All you need is a one-word change!
The problem is in the way you call whereami(), instead of self:: you should use static::. So class Foo should look like this:
class Foo {
public static function foobar() {
static::whereami();
}
protected static function whereami() {
echo 'foo';
}
}
In another word, 'static' actually makes the call to whereami() dynamic :) - it depends on what class the call is in.
Try to use singleton pattern:
<?php
class Foo {
private static $_Instance = null;
private function __construct() {}
private function __clone() {}
public static function getInstance() {
if(self::$_Instance == null) {
self::$_Instance = new self();
}
return self::$_Instance;
}
public function foobar() {
$this->whereami();
}
protected function whereami() {
print_r('foo');
}
}
class Bar extends Foo {
private static $_Instance = null;
private function __construct() {}
private function __clone() {}
public static function getInstance() {
if(self::$_Instance == null) {
self::$_Instance = new self();
}
return self::$_Instance;
}
protected function whereami() {
echo 'bar';
}
}
Foo::getInstance()->foobar();
Bar::getInstance()->foobar();
?>
Don't you have to overwrite the parent function foobar() too?
class Foo {
public static function foobar() {
self::whereami();
}
protected static function whereami() {
echo 'foo';
}
}
class Bar extends Foo {
public static function foobar() {
self::whereami();
}
protected static function whereami() {
echo 'bar';
}
}
Foo::foobar();
Bar::foobar();

Categories