The\worst\part\is\the\backslashes
But\you\get\over\it
What it looks like:
@[1] (namespace declaration applies to whole file) @[3-5] (All further declarations are in the namespace)
namespace SomeNamespace\MyFeature
class Widget {
...
}
- Think:
cd SomeNamespace\MyFeature
- Global namespace:
\
- Everything else is relative
- No, you can't
../
\SomeNamespace\MyFeature\Widget
- php:
use \Name\Path [as LocalAlias]
- Local alias defaults to the last component of the name
- It's just string replacement
\iFixit
-- all the things\iFixit\Framework
: Everything not feature specific\iFixit\Admin
: Features related to Admin- ... The future is unwritten
Classes should be grouped by feature, not type
-
PSR4: Namespaces should match directories
- Don't need to run
composer install
- Don't need to run
-
Functions can be namespaced and used just like classes
-
All classes are public :-(
Four Decisions
@ul
- Which backend:
Cache::getBackend()
- Key format:
"something-$id"
- Expiration time:
CACHE_DAY
- function to cache:
Guide::find()
@ulend
Like a promise, kinda.
- Represents a key/value pair in memcache
- Retreival:
->get()
- Disposal:
->delete()
Cached::byKey(
$key,
$valueFunction,
$expireTime = CACHE_FOREVER,
$cacheBackend
)
Also Cached::memoizedByKey()
@[3,16] (Which Backend) @[2,11-13,16] (Cache Key) @[4] (Expiration) @[7-10] (Value Function)
function getTitleCached($guideid) {
$key = getTitleCacheKey($guideid);
return Cache::getBackend()->getAndSet(
$key, 'getTitle', CACHE_HOUR)
}
function getTitle($guideid) {
return $title;
}
function getTitleCacheKey($guideid) {
return "guide-title-$guideid";
}
function deleteTitleCache($guideid) {
Cache::getBackend()->delete(getTitleCacheKey($guideid));
}
@[2-5] (Static factory function)
@[1-6] (Usage: function that returns Cached
)
@[8-12] (Callers get an object)
function cachedSomething(): Cached {
return Cached::byKey("something-$id",
function() use ($id) {
return Something::find($id);
}, CACHE_HOUR, Cache::getBackend());
}
// Then later:
$something = cachedSomething()->get()
// When things change:
cachedSomething()->delete()