1

I am working with a PHP project where I rename files, sometimes it takes time for these files to rename. I want to detect when they have finished renaming. I know in Objective C there are blocks that will perform a task and on completion give the ability to perform another task.

Here is an example that is a uiview animation:

[UIView animateWithDuration:kAnimationDuration delay:0 options:1 << 1 animations:^{
    myImageView.alpha = 0;
} completion:^(BOOL finished){
    [myImageView removeFromSuperview];
}];

My question is there something similar in php so that I can detect when a file has finished renaming.

I have done this with if statements, but I feel it's bad style:

if(rename($oldName, $newName)){
    //finished renaming
}
user906357
  • 4,575
  • 7
  • 28
  • 38

3 Answers3

1

You can use the concept of delegate here. Send a parameter to the rename function specifying the action to performed once the task is complete and perform that task at the end of the function. It is agreed that it is not as clean and efficient as the block approach but this will surely solve the problem.

Akshat Singhal
  • 1,801
  • 19
  • 20
1

What you can do i write you own method:

//method in your class

private function renameFile($oldName, $newName, $completion){
    if(rename($oldName, $newName)){
        if(is_callable($completion)){
            $completion();
        }
    }
}

Now in your class you can use the following:

$this->renameFile('testOld', 'testNew', function(){
    echo 'Completed!'; //completion method
});
Oritm
  • 2,113
  • 2
  • 25
  • 40
0

Theorically it is impossible, since PHP is not a multithreaded language but can be organised as a threading language using tools like pthreads (also take a look at this question). PHP goes on when it ends the execution of a function(line by line), but Objective C blocks work all at the same time(multithreaded). As @Ortim has stated above, you can use a variable from your remae function. if you need code feel free to comment.

Community
  • 1
  • 1
ilhnctn
  • 2,210
  • 3
  • 23
  • 41