Viewed   86 times

I have two classes: Action and MyAction. The latter is declared as:

class MyAction extends Action {/* some methods here */}

All I need is method in the Action class (only in it, because there will be a lot of inherited classes, and I don’t want to implement this method in all of them), which will return classname from a static call. Here is what I’m talking about:

Class Action {
 function n(){/* something */}
}

And when I call it:

MyAction::n(); // it should return "MyAction"

But each declaration in the parent class has access only to the parent class __CLASS__ variable, which has the value “Action”.

Is there any possible way to do this?

 Answers

1

__CLASS__ always returns the name of the class in which it was used, so it's not much help with a static method. If the method wasn't static you could simply use get_class($this). e.g.

class Action {
    public function n(){
        echo get_class($this);
    }

}

class MyAction extends Action {

}

$foo=new MyAction;

$foo->n(); //displays 'MyAction'

Late static bindings, available in PHP 5.3+

Now that PHP 5.3 is released, you can use late static bindings, which let you resolve the target class for a static method call at runtime rather than when it is defined.

While the feature does not introduce a new magic constant to tell you the classname you were called through, it does provide a new function, get_called_class() which can tell you the name of the class a static method was called in. Here's an example:

Class Action {
    public static function n() {
        return get_called_class();
    }
}


class MyAction extends Action {

}


echo MyAction::n(); //displays MyAction
Friday, October 14, 2022
4

Either use Interfaces and implement the methods manually or via Strategies. Or use Composition instead of Inheritance, meaning you let the Order_Product have a Order_Item and a Cart_Product.

On a sidenote: You could also consider making "shipping calculations" into it's own Service class that you can pass appropriate Product instances to.

Monday, December 12, 2022
1

No, but multiple inheritance is generally considered a bad practice. You should favor composition instead, so you just use instances of classes you wanted to inherit inside your class.

And now when I look into your question again, it's not even an inheritance issue, you should use composition. Maybe if you provided more detail what you expect that class to do, we should answer more accurately.

UPDATE:

You will need to create one method for each of these classes' method which you would want to use - it's called Facade design pattern. Or maybe you are not aware that you can call methods of inner objects like this:

$parent->subObject->subSubObject->wantedMethod();

Facade pattern:

http://en.wikipedia.org/wiki/Facade_pattern

in your case facade wouldn't be anything else than creating one class and define every single method you want to use, and inside that method calling any method of any of your inner instances. But i really don't see any benefit coming from this instead of calling instances and methods hierarchically

Tuesday, September 27, 2022
 
1

I see you are already using JQuery function in the code, and as far as Laravel use that as well you can manage this with JQuery. I did some changes so it should work as desired. Here is the example:

$("#add_row").on('click', function(e){
  let row_number = $('#products_table tbody tr').length;
  e.preventDefault();
  $('#products_table').append('<tr id="product' + row_number + '"></tr>');
  $('#product' + row_number).html($('#product' + (row_number - 1)).html());
    
});

$("#products_table").on('change', '#suajes_lista', function(e){
   $("#test").val(this.value); //get current selected option value
   alert('Row where the select was triggered has ID: ' + $(this).parent().parent().attr('id')); //get up on 1 level and get parent ID
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="card-body">
  <input type="text" id="test" name="test" value="" class="form-control"/>
  <button type="submit" id="add_row">Add new</button>
  <table class="table" id="products_table">
    <thead>
        <tr>
          <th>Descripción</th>
          <th>Suaje</th>
        </tr>
    </thead>
    <tbody>
      <tr id="product0">
        <td>
          <input type="text" name="descripcion" class="form-control" value=""/>
        </td>
        <td>
          <select id="suajes_lista" name="suajes_lista" class="form-control" style="width: 100px;">
            <option value="">-- Select --</option>
            <option value="1">1</option>
            <option value="2">2</option>
          </select>
        </td>
      </tr>
    </tbody>
  </table>
</div>

Hope it will help you understand the concept.

Monday, August 29, 2022
1

There's a simpler solution, working with Array Variables. At the top level, outside the For Each loop, declare a variable with an InitializeVariable action:

"Initialize_Items_variable": {
    "inputs": {
        "variables": [
            {
                "name": "Items",
                "type": "Array",
                "value": []
            }
        ]
    },
    "runAfter": {},
    "type": "InitializeVariable"
}

Inside the For Each, use a AppendToArrayVariable action. You can append the Response object of the Nested Logic App you just called.

"Append_to_Items_variable": {
    "inputs": {
        "name": "Items",
        "value": "@body('Nested_Logic_App_Response')"
    },
    "runAfter": {
    },
    "type": "AppendToArrayVariable"
}

Hope it helps.

Friday, December 2, 2022
 
sjfox
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :