0

I am developing my own package in Laravel and I am developing a huge number of helper to use with my package so that it can be used this way:

MyPackage::myfunc1();
MyPackage::myfunc2();
MyPackage::myfunc3();
....

The problem is MyPackage class (MyPackage.php) is becoming huge and the code is becoming very long. This bring hard maintainability to the file.

Is there anyway that I can split the class into a few files for easier maintaining? Or, is there any other way to do so?

Thank you.

user1995781
  • 19,085
  • 45
  • 135
  • 236

1 Answers1

1

As per request: File: MyPackage/Helpers/FooUtil.php

<?PHP
namespace MyPackage\Helpers;

class FooUtil
{

}

File: MyPackage/Helpers/BarUtil.php

<?PHP
namespace MyPackage\Helpers;

class BarUtil
{

}

Example how to use namespaces to seperate classes and how to use different classes in the same namespace. For more information read:

I generally advice you to read about PSR-0, which is used in Symfony/Laravel to properly support autoloading, composer and packagist: http://petermoulding.com/php/psr PSR in fact defines how to format namespaces in order to be applicable more globally.

In your example a proper namespacing might be:

Author\MyPackage\Helper\FooClass

Community
  • 1
  • 1
Luceos
  • 6,629
  • 1
  • 35
  • 65
  • Thanks a lot for your answer, but how can I get my package facade to link to the new class file? I tried by adding `$loader->alias('MyPackage', 'Vendor\MyPackage\AnotherClass');`, but it seems replacing the existing one instead adding. – user1995781 Apr 05 '14 at 15:45
  • what existing package are you replacing? You probably named your package the same as the one you're replacing. Also better to configure aliases in the config directory under config/app.php – Luceos Apr 07 '14 at 19:18