Method __call is running when come call to undefined method. I need to trigger when calling any existing method in class, something like __callAll.
The simpliest approach would be calling the method in every method, but I don't like this approach. The uses Zend framework.
Please advise me how to do it?
What you want isn't possible.
The only solution I can think of is the following:
Name all your functions, instead of myfunction, _myfunction.
Implement a __call method that wraps each of the function calls of the class to _myfunction.
Before executing the function, run your own code.
There's simply no other way to do this. I also wouldn't recommend using my way either but rather looking into some good code-design books because I bet what you want to do isn't necessary if you'd just have the right class design approach.
Related
In Dart every method gets override. In our business logic we don't need our this function will never override by its child classes so how can we make function
No.
Dart has no way to make a virtual method final/non-overridable.
Further, all classes introduce an interface, so someone can implement the same interface without even extending the original class.
In c# we have the protected accessor which allows class members to be visible on inherited clases but not for the rest.
In Swift this doesn't exist so I wonder what's a correct approach for something like this:
I want to have a variable (internal behavior) and and a public method using this variable on a base class. This variable will be used also on inherited clases.
Options I see
Forget about base class and implement variable and methods everywhere I need it. WRONG, duplicated code
Implement inheritance by composition. I'd create a class containing common methods and this will be used by composition instead of inheritance. LESS WRONG but still repeating code that could be avoided with inheritance
Implement inheritance and make variable internal on base class. WRONG since exposes things without any justification except allowing visibility on inherited clases.
Implementation Details for Base Class
I want to have a NSOperationQueue instance and and a public method to cancel queued operations. I add new operations to this queue from inherited classes.
In Swift the correct answer is almost always protocols and extensions. It is almost never inheritance. Sometimes Cocoa stands in our way, because there are classes in Cocoa more often than protocols, but the goal is almost always protocols and extensions. Subclassing is our last choice.
Your particular case is confusing because NSOperationQueue already has a public method to cancel queued operations (cancelAllOperations). If you want to protect the queue from outside access (prevent callers from using addOperation directly for instance), then you should put the queue inside another type (i.e. composition), and forward what you want to the queue. More details on the specific problem you're solving would allow us to help suggest other Swift-like solutions.
If in the end you need something that looks like protected or friend, the correct solution is private. Put your subclass or your friend in the same file with the target, and mark the private thing private. Alternately, put the things that need to work together in a framework, and mark the attribute internal. The Swift Blog provides a good explanation of why this is an intentional choice.
I am looking at a source code and it has a method named updateDisplayList. There are various methods in this source code with similar name. However I am interested in one particular updateDisplayList method. I want to check where this method is getting called. I have tried using CTRL+SHIFT+G in eclipse which returns me all the references of this method in that source code. However as there are many methods with same name, those references are also getting returned. How can I know where that particular updateDisplayList method is getting called?
As stated in the comments updateDisplayList() is a Flex component life cycle method. Practically every Flex component implements this method.
If you've modified this method in one class, lets call it ClassA, and you're also seeing the effects of this modification in other classes, it must mean that the other classes inherit from ClassA in some way.
To determine who's inheriting from ClassA, you can just search for that class name in your project. This will likely find the other class that you're looking for. However, there could be a series of classes that inherit from ClassA so you might have to look deeper than that (find all the classes that extend ClassA and then search for those classes). This might be a slippery slope and may not be fruitful.
Another approach is to set a breakpoint in the updateDisplayList() method in ClassA. As I mentioned, you'll hit this breakpoint frequently. In FlashBuilder/Eclipse, you can use the "expressions" window and inspect the value of this. If this is ClassA, it's not the droid(s) you're looking for, so let execution resume.
I'm sure there are a handful of other ways to get to the bottom of this. But updateDisplayList() is such a common method, there's no point in searching for that method name :)
I've got two view controllers that have some similar function. Some of them are IBActions. Is there a way to put them together(so that it's easier to fix bugs)? I've tried inheritance but it seems that it does not work.
#implementation ClassA
-(IBAction)f
{
//some code here
}
#implementation ClassB
-(IBAction)f
{
//some code here
}
My question is that is there a way that I write function f once? If there is a bug, I could fix it more quickly this way.
in inheritence, you can just declare in parent class, like abstract function, and for each child defination will be separte, now you have to do is this
make a method in parent class that performs the logic only,
make two ibactionn methods both in childs and perform the current child's UI tasks in that methods and use the parent's logic method, that u created above, to get the data.
inform if you get soltion or not.
Make a method that does the job and call it either from the IBAction or wherever you need it.
You can make utility kind of class where you can make class method for this.
And you can call this method where needed.
You Can make another singleton class or utility class as per your requirement. And make class method, for common functionality. So u can use this functionality any where from project.
Its good practice to make utility class or singleton class for some common functionalities.
In Objective-C, if I override a class method using a category, is there a way I can call the original method (the one that was overridden)?
I present you with three icky ways to do this in +(void)load. In every case, name your method MyCategory_method or so.
class_getMethodImplementation() and class_replaceMethod(). Store the old IMP, and call it directly. You need to get the method's type encoding. Note that you can just use a normal C function too...
class_getInstanceMethod(), method_getImplementation(), method_setImplementation(). As above, but you don't need to get the method's type encoding.
class_getInstanceMethod() on both methods, and then method_exchangeImplementations(). Call MyCategory_method to get the original implementation. This is the easiest way to do it.
Sometimes, it's the only reasonably easy way to make it do what you want...
EDIT: And only do this if you know what you're doing!
http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/ObjectiveC/Articles/ocCategories.html
Doesn't look like it is possible.
When a category overrides an inherited method, the method in the category can, as usual, invoke the inherited implementation via a message to super. However, if a category overrides a method that already existed in the category's class, there is no way to invoke the original implementation.
What this is saying to me is that if you override a method on a subclass via a category, you can call [super methodName] as you would normally, but if you override the base class method directly, you can't invoke the original.
If you dynamically provide the category override (see resolveInstanceMethod:), you can cache the previous method selector beforehand, and call that.