Custom Class / Extending Object?

Permalink
What's the proper 5.7 way to go about defining a custom class?

The examples in app.php show only how to override a built-in core class. For instance, if I want a Car class, where would the class file go so that it auto loads? Do I need to register it somehow in app.php?

Thanks for any pointers,

-Steve

Shotster
 
Shotster replied on at Permalink Reply
Shotster
Ok, so I guess my question boils down to, what is the equivalent of the "core" directory inside the "application" directory? I want to extend the foundation "Object" class, but it's unclear where to put the class file to exploit autoloading. Jobs, blocks, controllers, and such are no problem; but I don't see where to put a basic class file.

Anyone got any insights?

-Steve
Shotster replied on at Permalink Reply
Shotster
It almost seems that something like...

$symfonyLoader->addPrefix(NAMESPACE_SEGMENT_APPLICATION . '\\Core', DIR_APPLICATION . '/' . DIRNAME_CLASSES);


...should be added to...

concrete/core/Foundation/ClassLoader.php

...at around line 83, no? Or am I missing something?

-Steve
Shotster replied on at Permalink Reply
Shotster
Unless I'm misunderstanding something (which could well be the case), it seems to me that the class loader should be modified as per this pull request...

https://github.com/concrete5/concrete5-5.7.0/pull/166...

If I'm off base here, please ignore the PR and point me in the right direction.

Thanks,

-Steve
jordanlev replied on at Permalink Reply
jordanlev
I have no clue what the answer is, but I sure do hope there is a way to override specific methods of core classes in 5.7 (like we have in 5.6). One of the videos Andrew posted for 5.7 said that he's doing away with the "core" directory... but I hope we're not throwing away the baby with the bathwater here, because being able to override specific methods of core classes is a very useful feature.
Shotster replied on at Permalink Reply
Shotster
> I have no clue what the answer is, but I sure do hope there is a
> way to override specific methods of core classes in 5.7...

Jordan,

It turns out the pull request I referenced was accepted, and it fixed the override issue. Overriding in 5.7 is actually easier than in 5.6. See app.php in the config directory for examples in the comments.

-Steve
Shotster replied on at Permalink Reply
Shotster
Actually, I think I spoke too soon! I see what you're saying, Jordan. It's unclear to me, for instance, how one would simply override a method of the Page class (which is what I'm trying to do right now).

Creating a custom class based on a core class and using it in your own code is straightforward, but simply overriding/extending a core class (so that the rest of the core uses your implementation) is not. It seems the best (and proper) solution would be to fully exploit IoC and dependency injection throughout the core code -- i.e. using Core::bind() and Core::make() -- but that would entail some major refactoring.

Or am I missing something? Can anyone provide any insights or assistance?

Thanks,

-Steve