use new value of variables in extended class - php

This is my example code :
class Dad {
protected $name = 'Alex';
public function setName($string)
{
$this->name = $string;
}
}
class Son extends Dad{
public function showName()
{
return $this->name;
}
}
and i use that classes like this:
$dad = new Dad();
$dad->setName('John');
$son = new Son();
echo $son->showName();//it echo Alex but i need to echo John
I use many protected variable in parent and child class and this is just example.
How i can use new value from protected variables in child class ?

You have 2 objects. The default name in the object is 'Alex'. Therefore, when you create a new object and don't explicitly give it a name, it will use 'Alex' because that is the default.
$p = new Dad;
// name is Alex.
$q = new Son;
// name is Alex
$p->setName('John'); // Now the name of $p is John
$q->setName('Beto'); // Now the name of $q is Beto. $p remains unchanged.
This is the point of objects, that they are separate.
If you wanted a way to change the default name, then you could do this.
class Dad
{
protected $name;
static protected $defaultName = 'Alex';
public function __construct()
{
$this->name = self::$defaultName;
}
public function showName()
{
return $this->name;
}
public static function setDefault($name)
{
self::$defaultName = $name;
}
}
Now:
$p = new Dad; // $p->name is 'Alex'
Dad::setDefault('Bernie');
$q = new Dad; // $q->name is 'Bernie', $p is not changed
$r = new Dad; // $r->name is 'Bernie'
Dad::setDefault('Walt');
$t = new Dad; // $t->name is Walt
Hope this helps.

Related

How to achieve this: object->object->property

I see a lot of code where the calls are like this.
An example:
$person->head->eyes->color = "brown";
$person->head->size = 10;
$person->name = "Daniel";
How do I achieve what I wrote above?
That just means that $person, $person->head, and $person->eyes each have properties that are other objects. head is a property of $person, eyes is a property of $person->head, and so on.
So, when you set $person->head->size, for example, you are setting the size property of $person->head, meaning $person->head must be an object. Put differently, the statement $person->head->size = 10; means set the size property of the head property of $person to 10.
Example Code:
<?php
class Eyes
{
var $color = null;
}
class Head
{
var $eyes = null;
var $size = null;
function __construct()
{
$this->eyes = new Eyes();
}
}
class Person
{
var $head = null;
var $name = null;
function __construct()
{
$this->head = new Head();
}
}
$person = new Person();
$person->head->eyes->color = "brown";
$person->head->size = 10;
$person->name = "Daniel";
var_dump($person);
This outputs:
class Person#1 (2) {
public $head =>
class Head#2 (2) {
public $eyes =>
class Eyes#3 (1) {
public $color =>
string(5) "brown"
}
public $size =>
int(10)
}
public $name =>
string(6) "Daniel"
}
First thing: there are no method being called in your example.
To the answer:
This can be achieved by using another objects instances as attributes. Eg:
class Head{
public $size, $eyes, $etc;
}
class Person{
public $name, $age, $head;
public function __construct(){
$this->head = new Head();
}
}
$person = new Person();
$person->head->size = 'XL';
This is one way of doing it
You can also cast arrays as objects. This will generate stdClass instances with array indexes as attributes:
$person = array(
'name' => 'Foo',
'age' => 20
);
$personObject = (object) $person;
var_dump($personObject);
PHP method chaning is the secret, return on each getter method $this
class Person
{
public function head()
{
...
return $this;
}
public function eyes()
{
...
return $this;
}
}
$person->head->eyes->color = "brown";
https://en.wikipedia.org/wiki/Method_chaining#PHP

pass modified parent properties to child class

I created a parent class (A) and modified some of its public and protected properties.
I created a child class (B) that extends A.
I can see the parent properties at B instance after creating it.
Problem is: The inherited properties of B have the default values of A, from before I modified them.
I want B to hold the modified values of the inherited properties.
How?
class Dashboard {
protected $testBusinessesIds = '';
public function test_bids($a){
$this->testBusinessesIds = $a;
}
}
class DashboardDBHelper extends Dashboard{
protected $withoutTestBids = '';
public function __construct(){
if($this->testBusinessesIds != '')
$this->withoutTestBids = " AND B.`id`";
}
}
$d = new Dashboard();
$d->test_bids(23);
$dh = new DashboardDBHelper();
print_r($dh->withoutTestBids);
I see: '' instead of 'AND B.id'
You may need to put your property as static. Here's an example:
class A {
protected $value = 1;
protected static $staticValue = 1;
public function printStatic() {
self::$staticValue++;
echo self::$staticValue;
}
public function printNonStatic() {
$this->value++;
echo $this->value;
}
}
class B extends A {
public function printStatic() {
echo self::$staticValue;
}
public function printNonStatic() {
echo $this->value;
}
}
$a = new A();
$b = new B();
/* Class A */
$a->printStatic(); // 2
$a->printNonStatic(); // 2
/* Class B */
$b->printStatic(); // 2
$b->printNonStatic(); // 1
Static variables does not share the same class/object so if you modify the value it will be changed everywhere.

How to get all public properties of a class as json?

Consider following example:
<?php
class p{
public $name = 'jimmy';
public $sex = 'male';
private $age = 31;
// there should be more unknow properties here ..
function test(){
echo $this->name;
}
function get_p_as_json(){
// how can i get json of this class which contains only public properties ?
// {"name":"jimmy","sex":"male"}
}
}
$p = new p();
$json = $p->get_p_as_json();
echo $json;
Question: How to get all public properties of a class as JSON?
You just create another class q extends from p. And then the code looks like following:
class p {
public $name = 'jimmy';
public $sex = 'male';
private $age = 31;
// there should be more unknown properties here ..
function test(){
echo $this->name;
}
}
class q extends p {
function get_p_as_json($p) {
return json_encode(get_object_vars($p));
}
}
$q = new q();
$p = new p();
$json = $q->get_p_as_json($p);
echo $json;
$a = array();
$reflect = new ReflectionClass($this /* $foo */);
$props = $reflect->getProperties(ReflectionProperty::IS_PUBLIC);
foreach ($props as $prop) {
/* here you can filter for spec properties or you can do some recursion */
$a[ $prop->getName() ] = $a[ $prop->getValue()];
}
return json_encode($a);
Since the public members can also be accessed outside of the class..
Accessing members outside of the class
$p = new p();
foreach($p as $key => $value) {
$arr[$key]=$value;
}
Demo
Accessing the public members within the class by making use of ReflectionClass
<?php
class p{
public $name = 'jimmy';
public $sex = 'male';
private $age = 31;
// there should be more unknow properties here ..
function test(){
echo $this->name;
}
function get_p_as_json(){
static $arr;
$reflect = new ReflectionClass(p);
$props = $reflect->getProperties(ReflectionProperty::IS_PUBLIC);
foreach ($props as $prop) {
$arr[$prop->getName()]=$prop->getValue($this); //<--- Pass $this here
}
return json_encode($arr);
}
}
$p = new p();
echo $json=$p->get_p_as_json();
Demo
The best way to do this would not be to call a method of the class, per se.
However, you could initiate the following:
$myPublicMethodsInJson = json_encode(get_class_methods($p));
However, you would not be able to call get_class_methods from within the class because it will return ALL of your methods, private and public. When you call it from outside of the class it will only return the public methods.

Inheritance in PHP - Creating child instance and calling parent method

I have something like this:
class MyParent {
protected static $object;
protected static $db_fields;
public function delete() {
// delete stuff
}
public static function find_by_id($id = 0) {
global $database;
$result_array = self::find_by_sql("SELECT * FROM " . static::$table_name . " WHERE id=" . $database -> escape_value($id) . " LIMIT 1");
return !empty($result_array) ? array_shift($result_array) : false;
}
public static function find_by_sql($sql = "") {
global $database;
// Do Query
$result_set = $database -> query($sql);
// Get Results
$object_array = array();
while ($row = $database -> fetch_array($result_set)) {
$object_array[] = self::instantiate($row);
}
return $object_array;
}
private static function instantiate($record) {
$object = self::$object;
foreach ($record as $attribute => $value) {
if (self::has_attribute($attribute)) {
$object -> $attribute = $value;
}
}
return $object;
}
}
class TheChild extends MyParent {
protected static $db_fields = array('id', 'name');
protected static $table_name = "my_table";
function __construct() {
self::$object = new TheChild;
}
}
$child= TheChild::find_by_id($_GET['id']);
$child->delete();
I get this: Call to undefined method stdClass::delete() referring to the last line above. What step am I missing for proper inheritance?
You never actually instanciate the TheChild class, which should be done by
$var = new TheChild();
except in TheChild constructor itself.
So, the static $object field is never affected (at least in your example), so affecting a field to it (the line $object -> $attribute = $value; ) causes the creation of an stdClass object, as demonstrated in this interactive PHP shell session:
php > class Z { public static $object; }
php > Z::$object->toto = 5;
PHP Warning: Creating default object from empty value in php shell code on line 1
php > var_dump(Z::$object);
object(stdClass)#1 (1) {
["toto"]=>
int(5)
}
This object does not have a delete method.
And as said before, actually creating a TheChild instance will result in an infinite recursion.
What you want to do is this, probably:
class TheChild extends MyParent {
protected static $db_fields = array('id', 'name');
protected static $table_name = "my_table";
function __construct() {
self::$object = $this;
}
}
Edit: Your updated code shows a COMPLETE different Example:
class MyParent {
protected static $object;
public function delete() {
// delete stuff
}
}
class TheChild extends MyParent {
function __construct() {
self::$object = new TheChild;
}
}
$child = new TheChild;
$child->delete();
Calling "Child's" Constructor from within "Child's" Constructor will result in an infinite loop:
function __construct() {
self::$object = new TheChild; // will trigger __construct on the child, which in turn will create a new child, and so on.
}
Maybe - i dont know what you try to achieve - you are looking for:
function __construct() {
self::$object = new MyParent;
}
ALSO note, that the :: Notation is not just a different Version for -> - it is completely different. One is a Static access, the other is a access on an actual object instance!

How can I define a variable before create the class?

How can I define a variable before or while initializing the class?
<?php
class class{
public $var;
public function __construct(){
echo $this -> var;
}
}
$class = new class;
$class -> var = "var";
?>
If you mean instantiating the class, then use the constructor:
class Foo {
private $_bar;
public function __construct($value) {
$this->_bar = $value;
}
}
$test = new Foo('Mark');
You can do it 2 ways - see this example:
class bla {
public static $yourVar;
public function __construct($var) {
self::yourVar = $var
}
}
// you can set it like this without instantiating the class
bla::$yourVar = "lala";
// or pass it to the constructor while it's instantiating
$b = new bla("lala");
The first part you can only do with a static, but if you don't want to use a static, you'll have to initialize it via the constructor.
Hope that's what you were looking for...
$myVariable; // variable is defined
$myVariable = new myClass(); // instance of a class
class myClass {
protected $theVariable;
protected function myClass($value) {
$this->$theVariable = $value;
}
}
$theVariable = 'The Value';
$theClass = new myClass($theVariable);
echo $theClass->theVariable;

Categories