4

I'm having some trouble to call a function from a namespaced class in a different namespaced class. In the dummy example below I would like to know how to use Class2 within Class1. I'm getting the error:

Trait 'name1\name2\Class2' not found in class1.php

The code:

#file index.php

require "class1.php";
require "class2.php";
$class1 = new name1\Class1();
$class1->sayHello();

#file class1.php

namespace name1{
    class Class1{
        use name2\Class2;
        public function sayHello(){
            echo Class2::staticFunction();
        }
    }
}

#file class2.php

namespace name2{
    class Class2{
        public static function staticFunction(){
            return "hello!";
        }
    }
}

Thank you for any advice.

3
  • 1
    You have syntax error: not public static staticFunction() but public static function staticFunction() Commented Jan 16, 2015 at 19:02
  • 1
    Also in index.php: not just sayHello(); but $class1->sayHello(); Commented Jan 16, 2015 at 19:06
  • Yes @zavg thanks :) , as I commented in other posts, the errors were present because it's not the actual code. It was just a (really bad written :( ) dummy example. :S Commented Jan 16, 2015 at 19:19

3 Answers 3

2

Ok, so you've got several errors which I have fixed. Here's the working code you need:

# index.php
include "class1.php";
include "class2.php";
$class1 = new name1\Class1();
$class1->sayHello();

# class1.php
namespace name1;
use name2\Class2;
class Class1{
    public function sayHello(){
        echo Class2::staticFunction();
    }
}

# class2.php
namespace name2;
class Class2{
    public static function staticFunction(){
        return "hello!";
    }
}

Some explanations:

  • When in class definition the use is used for using traits and not namespace
  • In PHP namespace need not be enclosed in curly brackets
  • In PHP you include files with include, include_once, require, or require_once, and not import
Sign up to request clarification or add additional context in comments.

2 Comments

The errors were present because it's not the actual code. It was just a (really bad written :( ) dummy example. I accept the answer. Thank you!!!
cool, I added some more explanations which I hope will help you better understand what's going on :)
1

Inside your first class, your trait is calling class2 as use name2\Class2 but, you are still within the name1{} namespace, so in reality you are calling it as: use name1\name2\Class2

So, you need to change

use name2\Class2; to use \name2\Class2

Try this.

namespace name1{
  use \name2\Class2;
    class Class1{
        public function sayHello(){
            echo Class2::staticFunction();
        }
    }
}

#file class2.php

namespace name2{
    class Class2{
        public static staticFunction(){
            return "hello!";
        }
    }
}

Also, another tip: If you are separating your classes in separate files, you do not need to separate them as in they way you have done. Just call the namespace simple as:

// file1.php
namespace person; 
class name{}

//file2.php 
namespace address; 
class name{}

5 Comments

Hi @sam_io thanks for the response, that is not working either, I'm getting: PHP Fatal error: name1\Class1 cannot use name2\Class2 - it is not a trait
Please remove the use \name2\Class2 outside the class. @albertogarrido check my example again. Only traits can be called inside a class.
By taking out the "use" statement I get: Call to undefined function sayHello()
@albertogarrido you should call the function as $class1->sayHello();
Too many hours today :( The errors were present because it's not the actual code. It was just a (really bad written :( ) dummy example. I will vote up this solution as soon as I can. The actual working solution was use name2\Class2 outside of the class. Thank you so much for your help!
1

Why not drop the static method and just inject the class? Seems like going through extra work for something so simple. That's what function arguments are made for.

namespace name1{
  use \name2\Class2;
    class Class1{
        public function sayHello($Class2){
            echo $Class2->someFunction();
        }
    }
}

namespace name2{
    class Class2{
        public function someFunction(){
            return "hello!";
        }
    }
}

#index.php
include "class1.php";
include "class2.php";
$Class1 = new name1\Class1();
$Class2 = new name2\Class2();
$Class1->sayHello($Class2);
//hello!

1 Comment

Thanks @Tek but it's a dummy example, on the actual code has to be static function. Anyway for the doubt it's not relevant.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.