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.

Community
  • 1
  • 1
Literati Insolitus
  • 508
  • 2
  • 6
  • 13
  • 2
    How are you instantiating the object? `$object = new baseObject()` or `$object = new extendedObject()`? – Mike Purcell Nov 29 '11 at 00:33
  • @Digital Precision, this is merely in the class extension definition. If this were all in one file like so : $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. – Literati Insolitus Nov 29 '11 at 01: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. – hakre Nov 29 '11 at 01:07

2 Answers2

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();
Mike Purcell
  • 19,847
  • 10
  • 52
  • 89
  • 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. – Literati Insolitus Nov 29 '11 at 01:24
  • @LiteratiInsolitus: Updated code, check the `extendedFun()` method. Worked in my testing. – Mike Purcell Nov 29 '11 at 01:33
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.

hakre
  • 193,403
  • 52
  • 435
  • 836
  • 1
    Your solution works because it's all in the same file, the OP states it's across 2 files. – Mike Purcell Nov 29 '11 at 01:07
  • Was this all in one file; in the second file I also tried : namespace SecondNS; include 'NameSpace1.php'; use FirstNS; as per http://php.net/manual/en/language.namespaces.faq.php however the members of the base class still do not show up. – Literati Insolitus Nov 29 '11 at 01:17
  • Ahhh gotcha, hate when that happens. – Mike Purcell Nov 29 '11 at 01:18
  • @LiteratiInsolitus Two file example (eval is like include): http://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. – hakre Nov 29 '11 at 01:38