I have the following php code:
<?php
class ParentClass {
public $table_name;
function __construct() {
$this->table_name = strtolower(__CLASS__);
}
}
class ChildClass extends ParentClass {
function __construct() {
parent::__construct();
//And I also want to put here other codes
}
}
$parent = new ParentClass();
$child = new ChildClass();
echo $parent->table_name . "<br />" . $child->table_name;
?>
the result is
parentclass
parentclass
however I want it to be
parentclass
childclass
How do I achieve it?
Late static binding would solve my problem, however it cant be called statically of course.
http://php.net/manual/en/language.oop5.late-static-bindings.php
This works in PHP5.5 and upwards:
class ParentClass
{
public $table_name;
public function __construct()
{
$this->table_name = strtolower(static::class);
}
}
class ChildClass extends ParentClass
{
public function __construct()
{
parent::__construct();
// ...
}
}
$parent = new ParentClass();
$child = new ChildClass();
echo $parent->table_name . "<br />" . $child->table_name;
For reference, see http://php.net/manual/en/language.oop5.basic.php#language.oop5.basic.class.class.
Use static::class to get the name of the class using late static binding:
class ParentClass {
public $table_name;
function __construct() {
$this->table_name = strtolower(static::class);
}
}
This will result in the desired
parentclass
childclass
<?php
class ParentClass {
public $table_name;
function __construct() {
$this->table_name = strtolower(__CLASS__);
}
}
class ChildClass extends ParentClass {
function __construct() {
parent::__construct();
$this->table_name = strtolower(__CLASS__);
//And I also want to put here other codes
}
}
$parent = new ParentClass();
$child = new ChildClass();
echo $parent->table_name . "" . $child->table_name;
?>
Related
the code is self-explanatory, but the problem is, that I can't override the static variable through method calls, so the static variable of the class gives a value, but the value that I get through the objects is different.
Class dbEntity {
protected static $connection;
private static $name = "dbEntity";
public static function getName() {
return self::$name;
}
public static function setConnection($connection) {
self::$connection = $connection;
}
public static function getConnection() {
return self::$connection;
}
}
Class Patient extends dbEntity {
public static $connection = "patientConnection";
public static $name = "Patient";
}
$p = new Patient();
$p->setConnection("myConnection");
echo $p->getConnection() . "\n"; //myConnection
echo Patient::$connection . "\n"; //patientConnection
echo $p->getConnection() . "\n"; //myConnection
Ulrich is correct, if you change the following lines from self:: to static:: the code works as you're expecting it to.
public static function getName() {
return static::$name;
}
public static function setConnection($connection) {
static::$connection = $connection;
}
public static function getConnection() {
return static::$connection;
}
...
echo $p->getConnection() . "<br>"; //myConnection
echo Patient::$connection . "<br>"; //myConnection
echo $p->getConnection() . "<br>"; //myConnection
You need late static binding, see the official documentation:
https://www.php.net/manual/en/language.oop5.late-static-bindings.php
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;
}
I am trying to access the contents of a variable from another class. I have the code below, I am expecting to get 'test' returned, I get nothing.
I assume this is because it is getting $abc_rank as empty. It is required that the variable is populated in the function itself.
Therefore how can I get $abc_rank to hold that echo and output via the other class?
<?php
class class1 {
public static $abc_rank;
public function __construct() {
$this->add_text();
}
public function add_text() {
$this->abc_rank = 'test';
}
}
class class2 {
public function __construct() {
$this->display();
}
public function display() {
$test = class1::$abc_rank;
echo $test;
}
}
$go = new class2();
?>
I know I can do:
public static $abc_rank = 'test';
But the population of the variable must be in a function.
I have read some of the other related answers and can't seem to get this to work.
In class1 :
Replace $this->abc_rank = 'test'; with $this::$abc_rank='test';
($abc_rank is a static property)
In class2 :
In your display function : replace
$test = class1::$abc_rank;
echo $test;
with
$test = new class1();
echo $test::$abc_rank;
(class1 isn't static)
Full code here :
class class1 {
public static $abc_rank;
public function __construct() {
$this->add_text();
}
public function add_text() {
//$this->abc_rank = 'test';
$this::$abc_rank='test';
}
}
class class2 {
public function __construct() {
$this->display();
}
public function display() {
//$test = class1::$abc_rank;
//echo $test;
$test = new class1();
echo $test::$abc_rank;
}
}
$go = new class2();
you have to create the class1 to run the constructor of this class.
class class1 {
public static $abc_rank;
public function __construct() {
$this->add_text();
}
public function add_text() {
self::$abc_rank = 'test';
}
}
class class2 {
public function __construct() {
$this->display();
}
public function display() {
$test = class1::$abc_rank;
echo $test;
}
}
new class1();
$go = new class2();
I have this class
class TableInvoices extends WP_List_Table {
function prepare_items() {
$sumOwed = 'Anything';
$sumTotal = 'Something';
}
}
How can I echo $sumTotal outside the class ?
Thanks
EDIT:
#Eisa Adil solutions worked very well, also this worked:
class TableInvoices extends WP_List_Table {
public $sumOwed;
public $sumTotal;
function prepare_items() {
$sumOwed = 'Anything';
$sumTotal = 'Something';
$this->sumOwed = $sumOwed;
$this->sumTotal = $sumTotal;
}
}
$sum = new TableInvoices();
$sum->prepare_items();
echo $sum->sumOwed . '<br>';
echo $sum->sumTotal;
class TableInvoices extends WP_List_Table {
public static $sumTotal;
public function prepare_items() {
sumOwed = 'Anything';
self::sumTotal = 'Something';
}
}
echo TableInvoices::$sumTotal;
Use static variables. This would help you access methods and properties without instantiating an object.
Obviously it'll give undefined so you'll need to instantiate an object and run prepare_items() or statically access that too.
class TableInvoices extends WP_List_Table {
public static $sumTotal;
public static function prepare_items() {
self::sumTotal = 'Something';
}
}
TableInvoices::prepare_items();
echo TableInvoices::$sumTotal;
I have this parent class in PHP:
class parentClass{
public $table;
public function __construct(){
$this->table = "my_parent_table";
}
public function getName($id) {
$strQuery = "SELECT name FROM $this->table WHERE id=$id";
$result = mysql_query($strQuery);
if ($result) {
$row = mysql_fetch_object($result);
if ($row) {
return $row->name;
} else {
return false;
}
} else {
return false;
}
}
}
And I have also another class with inherits this one:
class childClass extends parentClass{
public $table;
public function __construct(){
$this->table = "my_child_table";
}
}
Then in another file I am doing:
$myObj = new childClass();
$name = $myObj->getName('1');
The problem now is that the getName function has a null table, so the variable $this->table is null, while I want it to be ""my_child_table" as long as I have a childClass object.
Does anyone know what I am doing wrong?
Thanks in advance
Not sure, but this look tricky:
class childClass extends parentClass{
public $table;
The parentClass already defines a $table, so it's likely that redeclaring it inside the child class will clobber the parent's version. You have to remove the declaration here. Also, public visibility doesn't really encapsulate the state very well; use protected in the parent instead.
public function __construct()
{
You should add parent::__construct() here (unless parent only sets $this->table, but even then it's good to add)
$this->table = "my_child_table";
}
}