What would be a good noop for PhP
Asked Answered
S

3

14

Sometimes I do not want to do anything.

I just want to have a statement so I can put break point.

In c and objective-c we have while (false);

Say I want to break a function

-(void)viewDidAppear:(BOOL)animated
{

    [super viewDidAppear:animated];
    self.navigationItem.leftBarButtonItem=nil;
    self.navigationItem.rightBarButtonItem=nil;


    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(updateDisplays) name: NotificationUpdateArroworLocation object:nil];

    PO(self.tableView.indexPathForSelectedRow);
    while(false);//I put break point here so program stop here.
}

In .net we have donothing (Not sure Did I make that up?).

What should we use in PhP?

Spoilage answered 9/1, 2013 at 9:0 Comment(13)
Did I read you right? You want to put a breakpoint in PHP?Kristopher
Why don't you use "while(false);" too?Swage
Well, I suppose I can put in regular places. Sometimes I want more control of where the breakpoint is. Check the objective-c code I wrote. I could do break point at PO(self.tableview.indexPathforSelectedRow). However, I prefer to break one line after it. Nah I use noop. Which is while(false);Spoilage
@skurton, I didn't think about it. I think in PhP while(false) should be followed by {} or something.Spoilage
@Swage if you're sure that work it can be the answer. You can turn that into an answer. I'll check that once I program in PhP again.Spoilage
Why should there be a need for a dummy function? If you have a decent IDE you should be able to put the breakpoint on the closing curly bracket.Snatch
@kmkapla - what's the problem with that? You can debug PHP just as easily as any other language.Firth
@Firth It’s a long time since I last did some serious PHP. What debugger exists? I will be happy to try them.Kristopher
I used zend. Yea I sometimes try the breakpoint at closing curly bracket not sure where it really breaks.Spoilage
@Kristopher - Zend provide a debugger, but xDebug is the more commonly used one. I certainly wouldn't contemplate doing any serious dev work without a debugger and all the other standard testing tools.Firth
@Firth thank you, I will checkout this xDebug stuff.Kristopher
This doesn't answer the full question (about noop) but for setting breakpoints where there isn't a line suitable to attach one to, I usually use xdebug_break()Colincolinson
What do you want to achieve with that? You can put a breakpoint on any lineLightsome
M
13

Would

assert(true);

be enough to attach a breakpoint to?

For the dev environment, assertions should be compiled and executed. For the prod environment, assertions should probably not be compiled nor executed. So you can leave them in. Which is useful!

http://www.php.net/manual/en/function.assert.php

Mcevoy answered 22/11, 2017 at 17:29 Comment(0)
P
14

You can use same trick and a lot of other features:

<?php
    while (false);
    // or empty output
    echo '';
    // or any expression without side effects
    5 + 5;

If you use XDebug, you can call xdebug_break() function directly in any place.

Placida answered 9/1, 2013 at 9:15 Comment(0)
M
13

Would

assert(true);

be enough to attach a breakpoint to?

For the dev environment, assertions should be compiled and executed. For the prod environment, assertions should probably not be compiled nor executed. So you can leave them in. Which is useful!

http://www.php.net/manual/en/function.assert.php

Mcevoy answered 22/11, 2017 at 17:29 Comment(0)
C
1

For PHP I use semicolon with a comment just to make it more explicit:

; // noop

Wikipedia says a lone semicolon on its own line was a classic way of doing it in C as well.

Caseworm answered 17/12, 2021 at 20:30 Comment(3)
Please add some explanation to your answer by editing it. Would XDebug hold at this line if you put a breakpoint on it?Lightsome
This is not a valid noop in PHP in my view, as a semicolon does not itself constitute a statement (and you can't place a breakpoint on it as OP requested)Hsinking
Also if you put ; in an empty if block,- static analyzer will still complain that Statement has empty body. So indeed, not a true minimal+"breakable" statement.Rushton

© 2022 - 2024 — McMap. All rights reserved.