2

I cannot seem to get this to work without the compiler yelling at me for being... stupid. But I have a class with another nested static class inside of it. I am trying to access it but I get a syntax error saying:

unexpected token ::

I am trying to call the class as follows:

myLibrary\myClass::nestedClass::myFunction()

The first set to '::' work just fine but the second pair are causing an error. Any idea of how I approach this? A lot of my libraries are written this way and I would very much appreciate if someone could help me!

Serguei Fedorov
  • 7,763
  • 9
  • 63
  • 94
  • Aren't nested classes considered `private`, making the double-use of the SRO (`::`) invalid? – Matt Jul 30 '12 at 15:39
  • Hmmm, good point. Is there a way to get around this by any chance? The library is written in C#, so they aren't actually private... – Serguei Fedorov Jul 30 '12 at 15:41
  • 1
    Write a public function within *myClass* that calls the desired function from *nestedClass* (I'm assuming it's `static`) – Matt Jul 30 '12 at 15:43
  • Sigh, I was hoping I didn't have to modify the class itself. Ok, I guess ill just have to have a wrapper class that does some rewiring. – Serguei Fedorov Jul 30 '12 at 15:44
  • That's just one solution; there may be others out there, but I try to avoid writing nested classes in the first place. – Matt Jul 30 '12 at 15:45
  • Phalanger uses standard PHP namespace separator in order to access nested classes. – Jakub Míšek Jul 31 '12 at 15:50

1 Answers1

4

There is actually a solution to this. Silly me! In order to get the nested class out of the class, its really simple. Look at how you can get a DataTable class out of the System .NET class:

  System\Data\DataTable

the same can be done with your library. For the code I have posted above, simply do:

  myLibrary\myClass\nestedClass::myFunction()

I replaced the first :: with a \ This way the compiler knows what I am looking for! Thanks for the suggestions, all of them would work wonderfully, in fact maybe are a little better code practice wise!

Serguei Fedorov
  • 7,763
  • 9
  • 63
  • 94
  • 1
    Yes, since PHP does not have nested classes, Phalanger developers had to specify how to access them. In PHP '::' operator accesses static members, and it would be performance and implementation overhead to use it to access nested classes. Classic PHP namespace separator was chosen instead. – Jakub Míšek Jul 31 '12 at 15:48