2

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.

3
  • 2
    How are you instantiating the object? $object = new baseObject() or $object = new extendedObject()? Commented Nov 29, 2011 at 0:33
  • @Digital Precision, this is merely in the class extension definition. If this were all in one file like so : <?php class baseObject { public $baseVar = 1; public function baseFun() {} } class extendedObject extends baseObject { public $extendedVar = 1; public function extendedFun() { } } ?> $baseVar and baseFun would be accessible from within extendedFun. This is to try to learn what namespace declarations are required to make $baseVar and baseFun accessible when accessing the namespace from a different file. Commented Nov 29, 2011 at 1:03
  • Namespaces are more or less unrelated to visiblity. They just play a role for a classname and allow you to prefix all class and function names within each namespace. Commented Nov 29, 2011 at 1:07

2 Answers 2

2

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();
Sign up to request clarification or add additional context in comments.

2 Comments

I've tried to include the original file, and I tried your code for file1 and file2 right now, @Digital Precision, however the baseClass members are not visible from inside the extendedObject definition.
@LiteratiInsolitus: Updated code, check the extendedFun() method. Worked in my testing.
1

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.

4 Comments

Your solution works because it's all in the same file, the OP states it's across 2 files.
Was this all in one file; in the second file I also tried : namespace SecondNS; include 'NameSpace1.php'; use FirstNS; as per php.net/manual/en/language.namespaces.faq.php however the members of the base class still do not show up.
Ahhh gotcha, hate when that happens.
@LiteratiInsolitus Two file example (eval is like include): codepad.viper-7.com/FlzgKO - you're just looking at the wrong points. Simplify your example and start w/o namespaces because actually namespaces aren't related to this. Whatever you try, you should also add your full code to the question. I don't see anywhere in your question where you access something nor are you giving any error messages. But those are absolutely important.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.