How to simulate non existing method error in __call?


You can trigger PHP errors manually using trigger_error:

trigger_error('Call to undefined method '.__CLASS__.'::'.$name.'()', E_USER_ERROR);

See http://php.net/manual/en/function.trigger-error.php

You have no MenuItem, whatever logic onOptionsItemSelected() would contain would fail (assuming there are multiple options).

Since you seem to have a need for shared code, move that piece of logic to its own method, then call that method from both onHomeClicked() and onOptionsItemSelected().

Eg

private void mySharedMethod (){
 //implementation
}

public void onHomeClicked (View v){
  mySharedMethod();
}

public boolean onOptionsItemSelected (MenuItem item){
  switch (item.getItemId ()){
    case android.R.id.home:
       mySharedMethod();
       return true;
    default:
       return super.onOptionsItemSelected (item);
  }
}
def some
  method_name = (self.public_methods - Object.instance_methods).sample
  public_send(method_name)
end

That will probably do it for you :)

Take a look at call_user_func() and call_user_func_array() functions, they can accomplish this in a more meaningful way:

call_user_func(array($this, $PageName));

By the way, this would do the trick with variable-variable:

$this->{$PageName}();

Constructors can't be async, but there are some ways to work around that.

Since you're using inheritance, you can't use the Factory Pattern from the above article directly, but you can combine the Factory Pattern for the derived class with a version of the The Asynchronous Initialization Pattern for the base class:

class BaseStorage
{
    protected BaseStorage(IStorageItem storageItem)
    {
        this.Name = storageItem.Name;
        this.CreationDate = storageItem.DateCreated.ToString();

        Initialization = setModifiedDateAndOwner(storageItem);
    }

    protected Task Initialization { get; private set; }

    private async Task setModifiedDateAndOwner(IStorageItem storageItem)
    {
        …
    }
}

class Document : BaseStorage
{
    private Document(IStorageFile storageFile)
        : base(storageFile)
    { }

    public static async Task<Document> Create(IStorageFile storageFile)
    {
        var result = new Document(storageFile);

        await result.Initialization;

        return result;
    }
}

This way, you have to be careful when implementing Document and other classes that inherit from BaseStorage, but you can only use those classes correctly (you have to await the Task to get the created instance).

Comments


Message :
Login to Add Your Comments .
Privacy Policy - Copyrights Notice - Feedback - Report Violation - RSS 2017 © bighow.org All Rights Reserved .