PHP parent:: not working? [closed] - php

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Why is the following code returning NULL? I am trying to get the parent method to return a value of a property inside the parent class but for some reason it is returning null.
class A {
public $greeting;
public function __construct($message){
$this->greeting = $message;
}
public function getGreeting(){
print 1;
return $this->greeting;
}
}
class B extends A{
public function __construct(){
}
public function getGreetingMessage(){
parent::getGreeting();
}
}
$classA = new A('Hello world');
$classB = new B();
var_dump($classB->getGreetingMessage());
In the parent method "1" is being printed so I know the method is being called

Because the greeting message is not set in class b. so greeting message returns null.

1) return parent::getGreeting();
2) $classA and $classB are different instances. Whatever you set in $classA is not accessible in $classB
3) constuctor of the parent is not called from classB and, moreover, you do not provide any arguments in initialization of classB (look at 2) above)
class A {
public $greeting;
public function __construct($message){
$this->greeting = $message;
}
public function getGreeting(){
print 1;
return $this->greeting;
}
}
class B extends A{
public function __construct($message){
parent::__construct($message);
// or $this->greeting = $message;
}
public function getGreetingMessage(){
return parent::getGreeting();
}
}
$classA = new A('Hello world');
$classB = new B('Hello world 2');
var_dump($classB->getGreetingMessage());

You have to call the parent's constructor in the child class, otherwise the parent constructor is not run and the class isn't properly initialized:
class B extends A{
public function __construct($message){
parent::__construct($message);
}
So you most likely want this
$classB=new B('Hello World');

Related

How to structure PHP classes [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
In file A,
Class A {
__construct() {
...
lots of code
...
}
public function P() {
code here
}
}
New A();
In file B,
Class B {
__construct() {
...
lots of code
...
}
public function E() {
I need to call function P in Class A
}
}
New B();
I would like to call the function P() in function E().
Is there any way to call a function in another class?
I found several ways such as dependency injection using __construct( A $aobj ) or "extends"
However, the class A was already instantiated in file A and there're a lot of things in __counstruct so I would like to know
1) the way of refractory technique
2) fancy way to call function E() in function P().
It really depends on how class B is related to A :
no link : the function in A should be static (it doesn't interact with a specific instance of A and its properties), and you can call A::P()
the class B is a child of A :
In B class definition, you have a class B extends A and in the constructor of B, you will have a parent::__construct() to call the constructor of A.
Then you can simply call $this->P() : as B is a subclass of A, and P is public (or protected), B inherits of all methods from A
The class B uses a object of type A`
Then, you must have a $a attribute in the class B, and a $this->a = new A(); in the constructor of B. You can call P with $this->a->P();
Or if you need only temporarily A in E(), you can construct a new object $a = new A(); and call $a->P(); in the code of E
Let's say that we have file a.php which contains class A:
class A {
public function __construct()
{
}
public function P()
{
echo "It is function P";
}
}
And file b.php which contains class B, in file b.php we first include a.php
include('a.php');
class B {
private $aobj;
public function __construct(){
$this->aobj = new A();
}
public function E(){
$this->aobj->P();
}
}
$b = new B();
$b->E();
Now open the browser and http://localhost/your-project/b.php and see the result.

Extending classes php [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
How can I get the value of class A extends Class B ?
class A{
private $a;
public function setA($a){ $this->a = $a; }
public function getA (){ return $this->a; }
}
class B extends A{
}
$a = new A();
$a->setA("Hello");
$b = new B();
echo($b->getA()); //empty
Given that it seems that you want to create an instance of class B that maintains the already-populated values of an existing instance of class A (while not just being a reference to it), you want to use PHP's clone feature.
class A{
private $a;
public function setA($a){ $this->a = $a; }
public function getA (){ return $this->a; }
}
class B extends A{
//
}
$a = new A();
$a->setA("Hello");
$b = clone $a;
echo $b->getA(); // output is "Hello"

How to call function in class to function another class in php? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I'm trying to call a funtion in class to function another class
with example this code:
class test1{
public function a(){
echo "Test";
}
}
class test2
{
public function b()
{
$obj = new test1();
$obj->a();
}
}
what is correct way??
I'm trying to call a funtion in class to function another class with example this code:
I assume you are not being able to call it?
<?php
class test1{
public function a(){
echo "Test";
}
}
class test2
{
public function b()
{
$obj = new test1();
$obj->a();
}
}
$t2 = new test2;
$t2->b();
?>
class test1{
public function a(){
echo "Test";
}
}
class test2
{
public function b()
{
$obj = new test1();
$obj->a();
}
}
$myTest2 = new test2();
$myTest2->b();

Call function across classes through controller [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have this controller class.
<?php
class controller{
function __construct(){
$this->db = new db;
$this->output = new output;
}
}
class db{
function get(){
return 'value from db';
}
}
class output{
function view(){
print $controller->$db->get();
}
}
$c = new controller;
$c->output->view();
?>
Ofcourse this is not working, but i think it gets across the idea of what i'm trying to do. How should i do this?
<?php
class controller{
function control($db, $output){
$data = $db->get();
$output->view($data);
}
}
class db{
function get(){
return 'value from db';
}
}
class output{
function view($data){
print $data;
}
}
$db = new db;
$output = new output;
$c = new controller;
$c->control($db, $output);
?>
Here you can use PHP5 Type Hinting. You can read more here
Code:
<?php
class controller
{
function __construct()
{
$this->db = new db;
$this->output = new output($this->db);
}
}
class db
{
function get()
{
return 'value from db';
}
}
class output
{
private $_db;
public function __construct(db $get)
{
$this->_db = $get->get();
}
public function view() {
return $this->_db;
}
}
$c = new controller();
echo $c->output->view();
In this example:
We're type hinting the class of db passing the $this->db instantiated object to the controller class __construct magic method.
And then on output class we're passing the actual classname db with instantiated variable and getting the method get() and storing the value to the private $db_ property and returning to view() method.
Hope it helps!

see variable from other class php [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
It's possible to do this if the class B NOT extended the class A but the class A call a new class B
class A{
public $lang;
public function __construct($lang) {
$this->lang=$lang;
}
public function new_B(){
return new B();
}
}
class B{
public function __construct() {
echo 'lang='.A::$lang;
}
}
$root=new A('eng');
$root->new_B();
You seem to have a mixup of concepts here. The $lang property of A is an instance level variable (since it is not defined as static), therefore you cannot access it statically as you are trying to. If you were to declare the variable as static then you would have access to it, but if you have multiple instances of class A that change it, it will change on the class level, rather than instance level.
Is A::$lang common to all A objects you will create? Then make this variable static. If not you can pass A::$lang as parameter to the B constructor. That is
class A{
public $lang;
public function __construct($lang) {
$this->lang=$lang;
}
public function new_B(){
return new B($this->lang);
}
}
class B{
public function __construct($lang) {
echo 'lang='.$lang;
}
}
Following is making A::$lang static:
class A{
public static $lang;
public function __construct($lang) {
self::$lang=$lang;
}
public function new_B(){
return new B();
}
}
class B{
public function __construct() {
echo 'lang='.A::$lang;
}
}
change the class B like this:
class B extends A{
public function __construct() {
echo 'lang='.$this->$lang; // you can use parent variables like this
}
}

Categories