This post is due to some difficulty I am having extending a class defined in a first namespace from a second namespace. Based on this post :
PHP how to import all classes from another namespace
I tried this :
File NameSpace1 :
<?php
namespace FirstNS;
class baseObject
{
public $baseVar = 1;
public function baseFun() {}
}
?>
File NameSpace2 :
<?php
namespace SecondNS;
use FirstNS;
class extendedObject extends FirstNS\baseObject {
public $extendedVar = 1;
public function extendedFun() {
}
}
?>
However $this in extendedFun can only access $extendedVar and extendedFun, not $baseVar and baseFun. I have also tried use FirstNS as ClassFromFirstNS; and class extendedObject extends ClassFromFirstNS however $baseVar and baseFun are still not accessible via $this. The information at http://www.php.net/manual/en/language.namespaces.rationale.php, http://www.php.net/manual/en/language.namespaces.definition.php and http://www.php.net/manual/en/language.namespaces.importing.php also did not seem to directly address this case.
Give this a shot:
// File1.php
namespace FirstNS;
class baseObject
{
public $baseVar = 1;
public function baseFun() {}
}
// File2.php
namespace SecondNS;
include 'File1.php';
use FirstNS;
class extendedObject extends FirstNS\baseObject {
public $extendedVar = 2;
public function extendedFun()
{
var_dump($this->baseVar); // Outputs 1
var_dump($this->extendedVar); // Outputs 2
}
}
// File3.php
include 'File2.php';
$object = new SecondNS\extendedObject();
$object->extendedFun();
I have no problems to get your code to work, it's not clear from your question where you've got a problem:
namespace FirstNS
{
class baseObject
{
public $baseVar = 1;
public function baseFun() {}
}
}
namespace SecondNS
{
use FirstNS;
class extendedObject extends FirstNS\baseObject
{
public $extendedVar = 1;
public function extendedFun()
{
echo $this->extendedVar, "\n"; # works
$this->baseFun(); # works
}
}
echo '<pre>';
$obj = new extendedObject();
echo $obj->baseVar, "\n"; # works
$obj->extendedFun();
}
Demo - Hope this is helpful.
Related
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.
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.
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
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.
The documentation says
The namespace keyword can be used to explicitly request an element from the current namespace or a sub-namespace. It is the namespace equivalent of the self operator for classes.
I need the equivalent of static instead, ie. if a class extends my class, the namespace of that.
This
return preg_replace('/.[^\\\\]+$/', '', get_class($object));
does it but it makes me sad.
Reflection provides an effective way of doing this via ReflectionObject->getNamespace(), check the following code:
namespace Foo {
class Bar {
public function getNamespace() {
return (new \ReflectionObject($this))->getNamespaceName();
}
}
}
namespace Baz {
use Foo\Bar as BaseClass;
class Bar extends BaseClass {}
}
namespace {
$bar1 = new Foo\Bar();
echo "ns1 is: ", $bar1->getNamespace(), '<br>';
$bar2 = new Baz\Bar();
echo "ns2 is: ", $bar2->getNamespace();
}
I don't think there's a "late static namespace" helper and you will indeed need to hack around.
e.g.
<?php
namespace ProjectFoo;
class Foo {
public static function ns() { echo __NAMESPACE__; }
public static function getNamespace() {
return static::ns();
}
}
namespace ProjectBar;
use ProjectFoo\Foo;
class Bar extends Foo {
public static function ns() { echo __NAMESPACE__; }
}
$foo = new Foo();
$foo::getNamespace();
print "\n";
$bar = new Bar();
$bar::getNamespace();
print "\n";