johnxx
August 25, 2023, 7:39am
1
What is the difference between setPackage and addPackage ?
I’ve only seen addPackage() being used.
Looking at the code, setPackage() ifself calls the function addPackage() .
* @param string $path The root path for looking up classes in this package.
* @param string|null $prefix Provide a string to define a package-specific table_prefix.
* @param string|null $namespacePrefix An optional namespace prefix for working with PSR-4.
* @return bool
*/
public function setPackage($pkg= '', $path= '', $prefix= null, $namespacePrefix= null) {
if (empty($path) && isset($this->packages[$pkg])) {
$path= $this->packages[$pkg]['path'];
$prefix= !is_string($prefix) && array_key_exists('prefix', $this->packages[$pkg]) ? $this->packages[$pkg]['prefix'] : $prefix;
}
$set= $this->addPackage($pkg, $path, $prefix, $namespacePrefix);
$this->package= $set == true ? $pkg : $this->package;
if ($set && is_string($prefix)) $this->config[xPDO::OPT_TABLE_PREFIX]= $prefix;
return $set;
}
/**
* Adds a model package and base class path for including classes and/or maps from.
*
* @param string $pkg A package name to use when looking up classes/maps in xPDO.
* @param string $path The root path for looking up classes in this package.
bobray
August 29, 2023, 11:08pm
3
This might help: Understanding addPackage, loadClass and getService | MODX , though it doesn’t deal with setPackage(), which I also have never heard of.
setPackage calls addPackage and then sets the “primary” package prefix to use when loadClass is called, whereas addPackage just adds a package to the array of packages to search through. It is irrelevant in xPDO/MODX 3.x as class namespaces and autoloading take precedence over the deprecated loadClass strategy.