Skip to content

Commit

Permalink
added setter method in container trait
Browse files Browse the repository at this point in the history
  • Loading branch information
tareq1988 committed Jun 6, 2023
1 parent c61f493 commit ac0e390
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 deletions.
22 changes: 8 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,10 @@ class MyPlugin {

public function __construct() {
// Register an instance
$this->register('my_service', new MyService());

// Retrieve the instance
$myService = $this->get('my_service');
$this->my_service = new MyService();

// Use the instance
$myService->doSomething();
$this->my_service->doSomething();
}

// Rest of your plugin code...
Expand All @@ -58,20 +55,17 @@ class MyPlugin {

public function __construct() {
// Add an action hook
$this->add_action('init', 'my_init_function');
$this->add_action( 'init', 'my_init_function' );

// Add a filter hook
$this->add_filter('the_title', 'my_title_filter');

// Execute an action hook
$this->do_action('custom_action', $arg1, $arg2);
$this->add_filter( 'the_title', 'my_title_filter' );
}

public function my_init_function() {
// Actions to be performed during 'init'
}

public function my_title_filter($title) {
public function my_title_filter( $title ) {
// Modify the post title
return $title . ' - Customized';
}
Expand All @@ -96,13 +90,13 @@ class MyPlugin {

public function some_method() {
// Log an informational message
$this->log_info('Some informational message.');
$this->log_info( 'Some informational message.' );

// Log an error message
$this->log_error('An error occurred.');
$this->log_error( 'An error occurred.' );

// Log a debug message
$this->log_debug('A debug message');
$this->log_debug( 'A debug message' );
}

// Rest of your plugin code...
Expand Down
12 changes: 12 additions & 0 deletions src/ContainerTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,16 @@ public function __get( $name ) {

return null;
}

/**
* Set dynamic property to container.
*
* @param string $name
* @param mixed $value
*
* @return void
*/
public function __set( $name, $value ) {
$this->container[ $name ] = $value;
}
}

0 comments on commit ac0e390

Please sign in to comment.