What is the best way to share custom PHP functions between blocks?

Permalink 1 user found helpful
Hi everyone,

I'm using concrete5.7.5.7

I'm writing different block types, and some PHP functions are the same between these block types.

What is the best way to share custom PHP functions between blocks?

 
WillemAnchor replied on at Permalink Best Answer Reply
WillemAnchor
in your blocks:
use Concrete\Package\YourPackage\Src\Yourclass.php

Yourclass::doSomething();


namespace your class:
namespace Concrete\Package\YourPackage\Src;
Class Yourclass {
public function doSomething() {}
}

save your class to:
packages/your_package/src/Yourclass.php
(mind the capital)
VonUniGE replied on at Permalink Reply
Thank you for your answer.

If I have no package and I use directly the application directory, is it correct to do :

In my block view.php:
<?php defined('C5_EXECUTE') or die('Access Denied.'); ?>
<?php
use Application\Src\Myclass
Myclass::doSomething();

and in the file containg the shared class
<?php
namespace Application\Src;
Class Myclass {
   public function doSomething() {}
}


saving my class in:
application/src/Myclass.php
WillemAnchor replied on at Permalink Reply
WillemAnchor
I never use the applicaton map myself, but it looks like that should work
You might however want to insert an extra map into scr to avoid conflicts.
Like application/src/yourname/Myclass.php
VonUniGE replied on at Permalink Reply
If I understand well the directory structure, everything under application/ is specific to a single website and everything under concrete/ is related to the core.

What kind of conflicts do you think of?
WillemAnchor replied on at Permalink Reply
WillemAnchor
It might override files in /concrete/src if it had the same name by coincidence

However, without testing this, I can't say 100% sure it would. It's just a precaution and good habit.
VonUniGE replied on at Permalink Reply
I understand and I think it's indeed a good precaution.

Thank you.