Function Visibility and Namespace Resolution in PHP
The error message "Function 'send()' does not exists" usually stems from a misunderstanding of how PHP handles function scope and visibility, rather than a missing file or syntax error. In your specific example, you are defining hookActionValidateOrder as a public method within a class, but then attempting to call a standalone function named send directly inside it.
While it is technically possible to call global functions from within class methods, the environment where your code resides likely dictates why this fails. If your send function is intended to be part of the same class, declaring it outside the class definition creates a global scope separation. When PHP executes hookActionValidateOrder, it looks for a method named send or a global function named send. If a namespaced class method is expected but a global function is provided, or if the function was defined after the method that calls it in the same file without proper scoping, the lookup fails.
To resolve this cleanly, you must ensure that send is treated as an instance method of the class containing hookActionValidateOrder. This approach encapsulates your logic and prevents accidental interference from global variables or external libraries. Here is how you restructure the code:
class OrderProcessor {
public function hookActionValidateOrder($params) {
// Assuming $order is available in scope or passed correctly
if (!isset($order)) {
return;
}
$invoice = new Address((int)$order->id_address_invoice);
$myStreet = $invoice->address1;
$myCity = $invoice->city;
$myPostcode = $invoice->postcode;
// Call the method using 'self' or 'this' to reference the current class instance
return self::send($myStreet, $myCity, $myPostcode);
}
public function send($a, $b, $c) {
// Implementation logic here
// Accessing properties of other objects remains straightforward
echo "Processing address: {$a}, {$b}, {$c}";
}
}
By moving send inside the class and prefixing the call with self::, you explicitly tell PHP to look for the method within the current class definition. This eliminates ambiguity about which send function is being invoked. Using static:: instead of self:: is also a valid option if send might be overridden in child classes, allowing for polymorphic behavior where the call resolves to the most specific implementation available at runtime.
One common pitfall developers encounter is mixing global functions with class methods without clear separation. If you intended send to remain a global utility function, ensure it is declared before any class that calls it, or use the fully qualified name if namespacing is involved. However, bundling logic like address processing into the same class usually improves maintainability by keeping related state and behavior together.
If this code snippet is part of a larger framework implementation, such as a Magento module, ensure that the file containing the class is properly included before the method is executed. PHP requires that functions be defined before they are called if they rely on global scope resolution. Additionally, check for typos in the function name; PHP is case-sensitive, so Send and send are distinct.
For developers working on complex object models where logic needs to be shared across different modules, consider extracting common logic into a dedicated service class or a trait. This reduces code duplication and makes the primary class more focused on its specific responsibilities, like handling order validation hooks.