0

I have a class which performs various functions. Within the class, I need to create an object, so, my intention was just to add another class to the file as shown below. My concern, however, is that the name of the additional class (i.e. mySubClass) might conflict with some other class name I am using. Since mySubClass will only be used in connection with myMainClass, is there a way to make it local (analogous to a local variable) to only myMainClass?

class myMainClass {
  public function myMethod() {
    $obj=new MySubClass();
  }
}

class mySubClass {
  public $prop1=array();
  public $prop2=array();
  public $prop3=123;
}
user1032531
  • 24,767
  • 68
  • 217
  • 387

2 Answers2

3

Use namespaces, just put the utility class into "myMainClassTools/mySubclass.php" and also add the according namespace.

Ulrich Eckhardt
  • 16,572
  • 3
  • 28
  • 55
0

In PHP there is actually no knowing way of making so called inner classes. See here: How do I Use Inner Classes in PHP?

But with PHP 5.4 you can use traits as shown here: http://php.net/manual/en/language.oop5.traits.php

And maybe this question for structs is interesting: Structs data type in php?

Community
  • 1
  • 1
algorhythm
  • 8,530
  • 3
  • 35
  • 47