Basic inheritance in laravel - php

I am very new to OOP. And i've read that a derived class can access the public and protected members of base class.
A.php
<?php
namespace App\Http\Controllers;
class A extends Controller
{
public $x=5;
public function index()
{...}
}
and B.php
<?php
namespace App\Http\Controllers;
class B extends A
{
public function index()
{
print_r($x);
}
}
why is $x not accessed from derived class?
I have this route:
Route::get('/B/index','B#index');
I got the error:
undefined variable x.

Make the following changes in your code:
class B extends A
{
public function get()
{
echo $this->x; // will echo the value in variable $x;
}
}
$obj = new B;
$obj->get();

Please change code as bellow. it will show result.
class A
{
public $x=5; //or protected $x=5;
public function index()
{
echo "A";
}
}
class B extends A
{
public function index()
{
echo $this->x;
}
}
$classB = new B();
$classB->index();
you can use :http://phptester.net/ to test online
I Hope help you

Related

Why relative static call not implement namespace of extended class?

Why only static call return 2?
it seems to me that a class call not by absolute name should depend on the current namespace in the class
<?php
namespace A {
class B {
static function test(){
echo 1;
}
static function check(){
B::test();//1 why?
self::test();//1
static::test();//2
}
}
}
namespace B {
class B extends \A\B {
static function test(){
echo 2;
}
}
}
namespace {
B\B::check();
}
B::test() is executed inside the A namespace and therefore takes the B class, that is provided by the A namespace. Therefore A\B::test is called. The context of the current class is not relevant for that.
The following example shows, that the heredity is irrelevant for this behavior.
<?php
namespace A {
class B {
static function test() {
echo 3;
}
}
class A {
static function test(){
echo 1;
}
static function check(){
B::test();//3
self::test();//1
static::test();//2
}
}
}
namespace B {
class B extends \A\A {
static function test(){
echo 2;
}
}
}
namespace {
B\B::check();
}
static::test returns 2 eventhough you are in the A namespace, because static takes the context from the calling super class and not the current namespace.
self::test returns 1, because self takes the context of the current class instead of the super class.

Same namespace cannot find constructor of class

I am trying to instantiate object of a class A that is of the same namespace of my current class C and I fail.
Both classes are in namespace App\Models.
This is the code of A.php:
namespace App\Models;
class A implements B
{
private $url;
public function __construct($url = "")
{
$this->url = $url;
}
}
This is the code of C.php:
namespace App\Models;
require_once 'A.php';
class C
{
private $url;
...some functions...
public function getC()
{
$test = A($this->url);
return $test;
}
...other functions
}
I get
Error: Call to undefined function App\Models\A()
in phpunit and I can't understand what I'm doing wrong.
I'm using PHP 7.0.24
By calling A() you're invoking A() as a function. Looks like you forgot a new:
class C
{
private $url;
...some functions...
public function getC()
{
$test = new A($this->url);
return $test;
}
...other functions
}
You've made a simple typo - it happens to the best of us.

How to call protected function?

How to call function lmn() without touching class B from class A
class A extends B{
public function abc(){
return "abc";
}
...
}
class B{
public function xyz(){
return "xyz";
}
...
}
class C{
protected function lmn(){
return "lmn";
}
...
}
please guide me for this
You can make some kind of proxy class that extends from C and provides a public method for access:
class ProcyForC extends C {
public function getLmn() {
return $this->lmn();
}
}
echo (new ProxyForC())->getLmn();

How to make an abstract variable static to a sub class

<?php
abstract class A {
public static $var = "A";
public function setVar() {
}
public function test() {
$this->setVar();
echo static::$var;
}
public function returnVar() {
return static::$var;
}
}
class B extends A {
public function setVar() {
static::$var = 'B';
}
}
class C extends A {
public function setVar() {
static::$var = 'C';
}
}
$class = new B();
$class->test();
$class2 = new C();
$class2->test();
echo "</br>";
echo $class->returnVar();
echo $class2->returnVar();
?>
What I'm trying to do is make the variable $var static to the class that extends abstract class A without having to re-declare it else where.
So say perhaps I create multiple objects from class B that extends A, I want all objects made from class B to share the same $var value.
Say I then create objects based on class C, they should all share the same value of $var...
This is the result I'm currently getting:
BC
CC
However, what I'm looking for is:
BC
BC
Thanks
Try it like that:
#setting
public function setVar() {
static::$var[get_class($this)] = 'B';
}
#getting in abstract
public function returnVar() {
return static::$var[get_class($this)];
}
#add this in the abstract class
public function setVar();

Get Class Name from Object inside Object

I have 3 classes..
class 1 :
<?php
include "two.php";
include "three.php";
class One{
public function __construct(){
$two = new Two($this);
$three = new Three($two);
}
}
$api = new One;
?>
class 2 :
<?php
class Two extends AOP {
public function __construct($obj){
//blablabla
}
}
?>
class 3 :
<?php
class Three extends AOP {
public function __construct($obj){
echo get_class($obj);
}
}
?>
But I want the result must output "One".
How to get class name from object inside object?
In your design you have to implement a getter in class two:
class 2 :
class Two
{
private $myObj;
public function __construct($obj)
{
$this->myObj = $obj;
}
public function getMyObj()
{
return $this->myObj;
}
}
then in class 3, you can retrieve class 1:
class Three
{
public function __construct($obj)
{
echo get_class($obj->getMyObj());
}
}
Use the keyword extends to inherit another class. Since PHP does not support multiple inheritances directly. You can get the class that you extend from with parent::$property; or parent::method();. So, you probably want your code to look more like.
// three.php
class Three extends AOP{
public function __construct($obj){
echo get_class($obj);
}
}
// two.php
class Two extends Three{
public function __construct($obj){
parent::__construct($obj); // Constructors do not return a value echo works
}
protected function whatever($string){
return $string;
}
}
// one.php
include 'three.php'; // must be included first for Two to extend from
include 'two.php'
class One extends Two{
public function __construct(){
// change this part
parent::__construct($this); // uses the parent Constructor
echo $this->whatever('. Is this what you mean?'); // call method without same name in this class - from parent
}
}
$api = new One;
I would not use your structure at all, but this should give you an idea of inheritance.

Categories