Wednesday, October 5, 2022
HomeWordPress Developmentphp - make an object out there after outlined in static...

php – make an object out there after outlined in static technique


In my plugin I am calling a static technique capture_details_form utilizing and motion hook upon a kind submission which creates an object from a category Type to seize the shape knowledge and shops the item as $form_obj . I then need this object to be out there to different objects to make use of the info for establishing db entries and interacting with APIs (for instance).

For some purpose I can not entry the $form_obj from different objects. As a substitute I get: PHP Discover: Undefined variable: form_obj in C:….

Instance code:

class Type {
  // properties that maintain kind knowledge
  public $var1;
  public $var2; //and so on

  public perform seize($kind){
   // extracts kind knowledge and units to object properties 

  }
}
class Order {
 public perform init(){
  // do one thing with captured kind knowledge
  echo $form_obj->var1; //provides error 'Undefined variable: form_obj in C:....'
 }
}
class Particulars {
 public perform set_up_order(){
  $order = new Order
  $order->init();
 }
 public static perform capture_form_data($kind){
    $form_obj = new Type();
    $form_obj->seize($kind);
    $this->set_up_order();
 }

}
add_action('form_submitted','capture_form_data');

I am unable to determine why the $form_obj is unavailable. I’ve thought-about utilizing globals however many suppose that that is dangerous apply? Any assist appreciated!

Edit

This has been closed at it isn’t particular to wordpress, though if I wasn’t hooking to an motion in wordpress I might have an answer (as I would not want to make use of a static technique). Additionally this is not a third occasion plugin its one which I am writing.

Edit 2

I solved this downside myself by utilizing static properties within the Type class as a substitute (though I might nonetheless prefer to know extra about object scope!):

class Type {
  // properties that maintain kind knowledge
  public static $var1;
  public static $var2; //and so on

  public perform seize($kind){
   // extracts kind knowledge and units to Type class static properties
   self::$var1 = my_form_processing_func($kind, 'field_1');
   self::$var2 = my_form_processing_func($kind, 'field_2');

  }
}
class Order {
 public perform init(){
  // do one thing with captured kind knowledge
  echo Type::$var1;
 }
}
class Particulars {
 public perform set_up_order(){
  $order = new Order
  $order->init();
 }
 public static perform capture_form_data($kind){
    $form_obj = new Type();
    $form_obj->seize($kind);
    $this->set_up_order();
 }

}
add_action('form_submitted','capture_form_data');

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments