Build reusable PHP components
Guilty Of PHP articles about Build reusable PHP components
9 articles
-
When Should a PHP Method Go in a Trait?
Use a trait for one clearly named behavior, then check that its inputs and expectations of the using class are visible before reusing it.
-
Declare a PHP Method Attribute and Read Its Arguments with Reflection
Define a method-only Label attribute, attach a named value, then inspect both its supplied arguments and its constructed object.
-
Define a PHP Function That Takes Arguments and Returns a Value
Build a reusable line-total calculation, then follow two calls from their supplied numbers through the function and back to the caller.
-
Define a PHP Abstract Class with a Shared Method and Required Child Method
Build a Label parent that prints a value, then give two concrete children their own values without duplicating the printing method.
-
Autoload MyApp\Catalog\Product from src/ with SPL or Composer
Map one namespaced class to its file with an SPL callback, then make the same mapping in Composer and include its generated autoloader.
-
Can You Declare a Named Class Inside Another PHP Class?
PHP does not support named nested class declarations. Declare the helper separately and pass an instance to the class that uses it.
-
Can a PHP Class Extend Two Classes? Use Interfaces, Traits, or Composition
A class gets one parent, but it can satisfy multiple contracts, reuse a trait method, and call another object for a separate service.
-
Create a PHP Class and Call Its Method on Two Objects
Define one Greeting class, give two instances different names, and trace how the same method reads each object's property.
-
Pass a One-Off PHP Object to a Function That Expects an Interface
Create an anonymous Formatter directly in a function call, pass it constructor arguments, and extend a base class when it needs a protected helper.
