Site icon Hip-Hop Website Design and Development

WordPress Custom Hook with Class method

The code described below works normally outside of wordpress, but in wordpress I get "warning expects parameter 1 to be valid callback function mycars ($ mycarsclass) not found". If I remove $ mycarsclass and leave only add_node, it works normally. How do I enable this for mycars ($ mycarsclass) to be valid? Is this possible in wordpress?

<?php
class mycarsclass {
  public $car1;
  public $car2;
function add_node($args) {
    echo '<div class="demo">I lake ' . $args['car1'] . 'and' . $args['car2'] . '</div>';
}
}
$mycarsclass = new mycarsclass();

function mycars($mycarsclass){
     
    $args = array(
    'car1' => 'Volvo',  
    'car2' => 'Toyota'
    ); 
    
    $mycarsclass->add_node($args);  
}

add_action('mycarsaction','mycars($mycarsclass)');
do_action('mycarsaction');
?>