PHP defines two SPL exceptions for invalid keys:
OutOfRangeException: Exception thrown when an illegal index was requested. This represents errors that should be detected at compile time.
OutOfBoundsException: Exception thrown if a value is not a valid key. This represents errors that cannot be detected at compile time.
As PHP isn't a compiled language the distinction between compile-time and run-time seems strange and thus I am finding it hard to understand which exception to use when.
Currently my understanding is that one should throw...
... OutOfRangeException if the key is fundamentally and inherently malformed, e.g. if an array is passed as a key.
... OutOfBoundsException if the key is generally okay, but isn't in some boundaries, e.g. if 100 is passed but 50 is the maximum key.
Is that understanding correct?
While PHP doesn't have a classic "compile time" (or a compiler that do a lot of static checks for that matter) I'd treat "compile time" as "rather static stuff I did wrong when writing the code" and "run time" as "my logic, input or validation was off at some point".
So my suggestion would be to treat it like this:
"Compile Time" / "OutOfRangeException": The error can always be fixed in the source code without or with very little logic.
I always take numbers from 1-10 and you put in 11
"Run Time" / "OutOfBoundsException": The error is due to wrong use at runtime.
You created me and told me to take values from 1 to 5 then you put in 7. Doesn't compute
or
You request an index that is not there because you didn't put it there like you should
Sample:
I'd expect an SplFixedArray to throw an OutOfBoundsException because it's size is dynamic and can chance at runtime while I'd expect something like a Calender::getMonthName to throw and OutOfRangeException because the number of month are definitely fixed at "compile/write" time.
Array object sample:
Say $array is an object that implements ArrayAccess you could throw an OutOfBoundsException in these circumstances:
$array['bar'];
$array[7];
As the values are what you could expect for ArrayAccess but it doesn't make sense in the case of an SplFixedArray(5). Alternatives would be DomainException or maybe RangeException
An OutOfRangeException in these cases:
$calendar->getMonth(15);
As putting an array or a new class there is definitely some bigger logic flaw in the code that usually results from a simple "oh, i put in the wrong variable" error by a programmer. An (maybe preferable) alternative would be UnexpectedValueException and good old InvalidArgumentException.
For cases like:
$array[array()];
$array[new StdClass];
some of the alternative exceptions seem more fitting.
Comparisons with the Java world on which exception to use when are not always applicable as Java Developers have an additions issue to deal with.
Checked/Unchecked exceptions. With many people arguing that everything that isn't a runtime exception has very limited use in Java / should not be used much internally) those names have lost some of their original meaning and intent.
My own take on this is:
LogicException
use for any mistakes the developer did, e.g. mistakes when programming/assembling the application. Those will hopefully be catched when the developer is running his/her UnitTests (which would then be the equivalent of Compile Time). Those exceptions should never occur on the production site as they are errors in the software as such.
RuntimeException:
use for any mistakes the user did or that result from invalid data put in the application at runtime. These are errors that might be anticipatable but not fully preventable by UnitTests, e.g. those can happen on a production site. Those exceptions could be stemming from incorrect usage or programming errors.
OutOfBounds
is IMO effectively what Wikipedia defines as Range of an Array in Range in Computer Programming, namely:
When an array is numerically indexed, its range is the upper and lower bound of the array. Depending on the environment, a warning, a fatal error, or unpredictable behavior will occur if the program attempts to access an array element that is outside the range.
In other words, when you have an array with indices [0,1,2] anything but [0,1,2] is out of bounds. Note that Bounds can also apply to other data types as well, e.g. trying to access the 7th character in a 5 character string would also be OutOfBounds.
OutOfRange:
this one is a tough cookie. In C++ OutOfRange is a generic exception extending LogicException (just like in PHP). I am not sure if the example given is easily translatable to PHP code though or my definition of Compile time above. Mainly because no one would first init a new Vector(10) and the immediately try to access it at(20).
In Java OutOfRange seems to refer to Range in the mathematical sense
The range of a function is the possible y values of a function that result when we substitute all the possible x-values into the function.
One reference I could find has it extending RuntimeException though, so I guess looking into other languages wont help to solve this mystery. There is also an open bug report about the SPL Exceptions in general, stating that
OutOfRangeException (value is out of range) is LogicException, should be: RuntimeException
But if this is correct, then how is OutOfRange different from DomainException? And if your own definition is correct, then how is OutOfRange different from InvalidArgumentException?
To cut a long story short: I dont know what OutOfRangeException is supposed to be for.
The answer to your question is quite elusive to me also. However, here are some things to think about:
If an array is passed in when we are expecting a valid array key, we also have InvalidArgumentException because it is not the proper argument type.
We could also throw a DomainException because arrays are not in the domain for array keys.
In php, you generally can't detect types at compile time because of late static binding. They purposefully delay binding of variables to runtime.
How I handle this situation:
Throw an InvalidArgumentException if a variable is passed in to any function where it the argument is not the correct type. I still do this when working with arrays.
Throw an InvalidArgumentException if null was passed in when it shouldn't be. This one really could be a lot of things because null isn't typed. To keep error code checking simple, I simply stick with the invalid argument.
Throw OutOfBoundsException when an index is not in the correct range, just as you suggested.
Throw BadFunctionCallException if a user-supplied function as a parameter does not have the correct form. If your structure inside is an array, it makes sense that they could pass in a function to modify it, so this comes up occasionally.
Generally, I can use just these three exceptions to represent all errors that occur outside of special resources (Network and database connections would be special resources). The third one seems to have been cropping up more often, but primarily I've just dealt with the former two.
It is quite simple:
OutOfRange mean "Your requested key is not within the index of a set defined in code."
OutOfBounds means "Your requested key is not within the index of a set defined by loaded configuration."
The confusion in this case comes from a couple of factors. First, PHP is in fact compiled into bytecode. There are several execution environments where this compiled form persists in a relatively permanent form on the server. However, the OutOfRangeException/OutOfBoundsException issue is not about that, it's about a categorization error made by the people who documented these specific exception classes.
Since PHP is dynamically typed, it's often impossible to actually check ranges and even types at compile time. The manual states that OutOfRangeException should be raised at compile time and OutOfBoundsException should apply at runtime, which is an erroneous distinction to make in this context.
Both manual entries use unclear language of what an illegal index means, but looking at the usage of their parent classes gives some clues: LogicExceptions are extended by classes like DomainException, InvalidArgumentException, and LengthException, whereas Runtime exceptions are things such as UnexpectedValueException, OverflowException and UnderflowException. From this pattern one can infer that OutOfRangeException should probably be applied to illegal key types, and OutOfBoundsException should apply to index values that are of the correct type but are not within the bounds of their container.
There was some discussion on the PHP dev list about these categorizations being wrong, but the issue goes deeper than that. In practice, both exceptions can only actually be are raised at runtime. You can use the vagaries of the documentation to squeeze out a distinction between invalid key types and invalid index values, but at this point we're talking about a bug in the PHP documentation.
If you get an unexpected index. ie you expect a string but end up with an integer or vice versa. Then you should throw an UnexpectedValueException exception.
If you get a proper type of index but it doesn't exist. Then raise a warning (trigger_error) and proceed on. This is not expected to stop programming flow.
If you have an object that is iterable or supposed to be iterated over a range and it reaches it's limit (ie the end of a file), then you should throw an OutOfBoundsException.
Anything else is a candidate for OutOfRangeException.
In layman's terms. An OutOfBoundsException is something normal. It's not that serious. It's something that happens often and should be taken care of. it can be used by iterators to keep reading data till there is no more to read. It is a logical error. Made by someone using the code not by someone writing the code.
An OutOfRangeException is something serious. Someone should look at the source code. Someone should find out what happened. This is important. Theoretically this was never supposed to happen. Call 911. It is a compile time error. Made by the dummy programmer.
Out of range exceptions are written by programmers to guard against mistakes by other programmers. Or maybe yourself in the future. If you feel like something like that could never happen then use Out Of Range. Use Out of Bounds for something likely to happen.
Related
[Update 18.01.14]
This question was marked a duplicate of Intermittent PHP Abstract Class Error . However, they are different in much of the bug appearance as follows.
That link only have contains x abstract method error. But mine have more types, and the errors are more weird (see the errors below, even containing some control characters).
In that link, the questioner said disabling opcache will make it happen less frequently. But in my case I did never enable the opcache but these bugs happened.
[/Update]
I am using Apache, PHP, Laravel to develop my web backend. But several times a day, errors will occur saying some php built-in functions are undefined, but at other time when it is not mad, these functions are obviously defined and work well. (So it is not because I call something really undefined!)
There are even stranger things. The "undefined" things can be a php function, system constant, or even some strange strings which look like regex, or even control characters in ASCII! The examples are as follows.
Declaration of PDO::() should be compatible with PDO::?)[CHAR1][CHAR2] where the [CHAR1] and [CHAR2] are SOHand ETX in ASCII.
Call to undefined method #^\\{\\w+\\}#::format()
Call to undefined method DateTime::format()
Undefined class constant 'PDO::ERRMODE_EXCEPTION'
(Other types of errors) Class Doctrine\DBAL\Driver\PDOStatement contains 2 abstract methods and must therefore be declared abstract or implement the remaining methods (Doctrine\DBAL\Driver\Statement::errorInfo, Doctrine\DBAL\Driver\ResultStatement::closeCursor)
Here are some more information: It usually happens every several hours, but can happen in half an hour or do not happen for half a day. My server handles about 1 http GET or POST request every second from one of the 6 remote machines.
This annoys me for a very long time and I would appreciate it if anyone could help me! Thank you so much!
I read an interesting quote on a Python forum that it's "easier to ask for forgiveness than it is to ask for permission". I'm not too familiar with the language so I can't say whether this is garbage. From working with .NET, it is my understanding that a try catch is an expensive operation and should be there for exception cases only. Kind of like a safety net.
Is there any merit to this type of behaviour with php? As in, is it quicker to read from a file inside a try catch vs checking to see if the file can be found / read before performing the read operation. I can see how it makes the code easier to maintain, but what are the perfoance implications. Is it more of a waste to check when 99.9% of the time the check is pointless.
To the best of my knowledge, this is not a standard practice in PHP. This is, in large part, because most PHP builtins threw PHP errors -- not exceptions! -- prior to PHP 7.0. PHP errors could not be caught by PHP try/catch blocks, making it mandatory to use explicit checks for functions that could fail.
If your code needs to be compatible with PHP 5.6 or earlier, you can't use this convention. It may be worth experimenting with if you can mandate PHP 7.0 or later for your code, but I don't know what implications this may have for performance.
Try and catch by itself is truly quite expensive. Even when it is not used (i.e. no exception is generated), there is still some overhead that is "embedded" into the generated assembly. As for simple check whether the file exists and can be accessed, same thing happens: both of the things are system calls that are generally even more expensive. You should, however, also consider that the read operation on a file is a system call as well.
Now the question is narrowed down to what is more expensive: 2 system calls or 1 syscall with the exception handling. My assumption here (which I am somewhat confident about) is that the latter will be more expensive in case where the exception is actually being thrown, but faster in other cases. Since you've indicated that there will, in most cases, be no problems with accessing the files, it may be the case that you "should" use the exceptions. It is easier to deal with errors and does make the code a little bit more pretty (although it is arguable).
A little bit more on the code side of things: it totally depends on what your framework looks like now and what you want it to look like. You can, in fact, create a class that will deal with all this stuff in any of the two ways and they both will look clean (if done properly) and easily maintainable. If, however, you don't feel like keeping it in a separate class (or a separate file\function\universe), I would go for the exception handling.
Finally, as said before, the exception handling will generally be faster in you case, but ask yourself a question: How much of a problem is it? Do you perform the operation once per page access or do you do it multiple times? Will the page be accessed often or just once a day? How much of an impact will this create compared to all the other pages\algorithms that you have. There is a good concept know as 90/10 rule: 90% of the code is executed 10% of the time and vice versa. If this particular code is in these 90%, you shouldn't even worry about the performance as your concern should be the other 10% of the code. (You can read more about this here)
And one more thing: as noted by duskwuff, whatever is written here holds for PHP version >= 7.0
from some articles I read on general programming concept. I was made to know that "syntaxs are the formal rules that governs the construct of valid statement in a language" while "semantics are set of rules that give meaning to a statement of a language". from the defination of semantics, I feel it is similar to logic, if not, then please I want to know the difference between logical error and semantic error?
There seems to be lot of confusion around the definition of these terms, but here's my understanding:
Syntax relate to spelling and grammar.
Logic relate to program flow.
Semantics relate to meaning and context.
If the code fails to execute due to typos, invalid names, a missing parenthesis or some other grammatical flaw, you have a syntax error.
If the syntax is correct but a piece of code is (inadvertently) never executed, operations are not done in the correct order, the operation itself is wrong or code is operating on the wrong data, you have a logical error. Using a wrong conditional operator is a common example, so is inadvertently creating an infinite loop or mixing up (valid) names of variables or functions.
If both your program logic and syntax is correct so the code runs as intended, but the result is still wrong: you likely have a semantic error. Confusing a metric input value for an imperial value will get you there. Nothing wrong with the program, except that miles and kilometres don't add up, so your area calculation throws out the wrong number. Having a race condition is another common example.
The answer here depends on the book you are reading or the class you are in. In many areas of Computer Science, there is absolutely no difference between a Semantic Error and a Logic Error. Both mean that the program compiled, but the output was wrong. Just as often, they mean two different things. A simple example is intending to use X+1 in your program, but you typed X-1. That is a Logic Error. If you typed X+true, it would be Syntax Error if the language allowed it to pass through the parser, but the result of X+(boolean true) wasn't the same as X+1. Personally, when it comes to poorly defined terms such as this, I let people define them how they like and just remove the errors from my programs, regardless of what kind of errors they are.
Basically differentiating semantic errors and logic errors is contradictory as, first both in programming produce an result that is not to the expected function of the program and secondly logic errors result in semantic error as the program runs against expected functioning.
Just Google it thousands of answer will be in front of you with brief.
Semantic error is related to the meaning of something. it mean that it is a violation of the rules of meaning of a natural language or a programming language , suppose we are using the programming statement improperly ..the semantic error will be detected at compile time.
and the logical error is that Errors that indicate the logic used when coding the program failed to solve the problem. The logic error will not cause the program to stop working but our desired result will not be get.
if you want to see the example go to this site.....
http://newtutorial2012.blogspot.com/2012/07/differentced-between-synataxsemantic.html
Ruby and some other languages have a very convenient feature: symbols. They look like in-place constants. Now I wonder if the following approach by simulating symbols in PHP with an at sign before an unquoted string is a valid approach.
$array = [#key => "value"];
echo sprintf("%s PHP symbols with a %s\n", #testing, $array[#key]);
I understand there are certain drawbacks against formal constants and the like, which are same as for Ruby's symbols: consider typing errors. Are there any other considerations against using this approach?
If by "valid" you mean "can be run", then yes, it is a valid approach (but by that standard, it is also valid to make all of your strings into HEREDOC's). But simply because PHP will accept the syntax, does not mean that the syntax is without problems.
The first I can think of are that
You are actively suppressing an error, which costs processing time
Your co-workers will need an explanation as to what is going on, which costs developer time
You are working against the natural definitions of the language (PHP simply isn't Ruby)
Since you have to use a sigil for variables anyway, you're not actually cleaning the code.
You are suppressing an error (a notice, to be exact). not only this costs processing time as mentioned in cwallenpoole's answer, but also the error is there for a reason. The reason is:
Notice: Use of undefined constant hello - assumed 'hello' in ...
You are relying on some constant being undefined - which is exactly what the notice is trying to tell you. If a constant of that name is defined, you will grab its value instead.
In Ruby, :__LINE__ is something quite different from __LINE__. The former is a symbol - it equals itself no matter where you use it. The latter is a number, and a magical variable that changes its value on every line. In PHP, #__LINE__ is the same as __LINE__, because there is no error to suppress. Oh, and there's one special "symbol" that is extra-fun to debug: #exit, AKA #die.
In Ruby, you can use all sorts of symbols including operators and keywords. These (and many more) are all valid: :+ :* :< :<< :[] :[]= :while :case :x=. With a pair of parentheses, you can even use symbols like :case= and :while=. In PHP, none of these work. You'll end up with a parse error. It won't even be suppressed. The only exception is #[] in PHP 5.4, which produces an empty array. On the other hand, lots of PHP expressions are not valid Ruby symbols: #(1+1) === #2 #1 == #'1'
Ruby's symbols are not equal to anything else. This is the purpose of their existence. Sure, they have some nice properites like to_s and to_proc, but their original purpose is to serve as identifiers separate from any possible user input. This is sorta nice for example if you are using symbols to represent tokens in a lexer stream, such as [:lparen, 1, :plus, "rparen", :rparen]. In PHP, undefined constants are strings. In Ruby, ?test != "test". In PHP #test === "test" (assuming you dindn't define a constant named "test" to equal something else).
you can't even assume non-magic constants won't change. You can't even attribute to malice what can be explained with bad coding. Nothing like that is of worry in Ruby:
//in library code:
$this->status = #done; // bad
//outside library code:
define('done', "no"); // very bad
define(#done, "yes"); // even worse
echo #no; // prints "yes"
//in library code:
if($this->status == #done){
//won't execute
}
echo #die;
echo "this won't get printed!";
You shouldn't rely on constants being undefined, and you shouldn't use error suppressing to hide the error messages telling you that. You shouldn't use special notation to pretend two things are not equal when they are. Also, can you trust the users of your library to not redefine constants at runtime?
Warning:
The following answer contains analogies that are meant, purely to illustrate a point. Under no circumstances do I mean to even suggest you contemplate the possibility of someone (you or anyone else) actually sitting down and doing the things I mention. That way madness lies
Though other answers have explained the main issue with using #<str>, It supressing a notice, it's important to stress this a bit more.
When using the supressing # (of death) the notice does not magically dissapear it is still being issued. After a while logs will get clogged with notices, making it harder to find that one fatal error that could be in there. Even if there is no fatal error, it still slows the code down.
Why encourage people to write code that throws notices? Just because you like the ruby syntax? come on, if you don't like a language, don't use it. I know: legacy code, you have to... well then, do it, don't try to make it feel and look like Ruby. They're not the same language. Everything that reminds you of the fact that you're working with a different language should be seen like a tool. Different languages require different mindsets, and different ways of thinking about a problem.
Imagine writing Lisp, but change the syntax to SQL queries. How much bad code will that generate. The syntax forces you into an SQL mindset, whereas you should be thinking in functions.
But for God's sake, don't that way madness lies!! It's a fools errand, it's even worse than parsing HTML with regex. It'll make even Cthulhu cry like a little girl
Oh, and # not being used to supress errors once it's in common usage? Do you really believe that? So you expect a lot of people to write bad code, until some IDE plugin is released that doesn't complain about the abuse of the # sign. And then, you expect the PHP contributors to take notice, and find a new operator to supress errors?
Honestly. I don't want to be rude, but that's like expecting Microsoft to release the source of windows8, because some people have gotten used to linux being open source.
Another thing: As I said, suppressing notices isn't going to help you when debugging the code. It's well known that PHP has way to many functions (and reserved keywords) in its core/global namespace. If you, and your co-workers get in the habit of abusing the # operator, you could just end up with code like this:
$foo[#die] = [#exit, #constant];
Have fun debugging that onholy mess of unclear errors. Honestly...
The key in your code would be seen by PHP as an unknown constant.
In most languages this would halt the compiler, but PHP mutates it into a string in an effort to keep running. It throws a warning, but keeps going anyway. This is just bad, and you really don't want to be doing it, much less doing it deliberately.
# in PHP is for suppressing errors. It does nothing else.
Your #key will still be bad practice just as key would be and would function in exactly the same way, but the # will hide the error message that would normally be generated.
Using # error suppression in PHP is bad practice for a whole bunch of reasons (‡ see note below), but using it as a way to hide deliberately bad code is terrible. Please don't do this.
The fundamental point here is that you're using PHP, so you should write PHP code. Trying to write Ruby code in PHP is never going to work.
You should work with the language you're using, not against it.
‡ For some thoughts on why error suppression is bad practice, you may want to read this: Suppress error with # operator in PHP
One final thought: This thing of PHP converting unknown constants to a string exists in the language purely for legacy compatibility reasons; it's one of the really awful bits of bad language design that date back to the early days. A lot of the other bad stuff from early PHP has been deprecated in recent versions; this hasn't yet, but there's no good reason for it still to exist, so I kinda hope they find a way to deprecate this "feature" too. If they do, that will instantly stop your idea from working, regardless of any merits it may have.
The the PHP manual page about the exit construct states:
exit — Output a message and terminate the current script
Based on that, would it be correct to think that it violates the single responsibility principle?
No, because exit is a procedural language construct, not a member function of any class. The single responsibility principle is supposed to apply to object-oriented programming, something which does not encompass the entire basis of the PHP language (only a portion of it).
Technically, yes. However the exact violation isn't that bad... The Single Responsibility Principle is actually an abstract principle that can be applied to any unit of code. In Steve McConnell's Code Complete 2, he actually talks about this as cohesion. In practice, this is a more specific form of the single-responsibility-principle used for routines.
The most desirable kind of cohesion according to him is functional cohesion where a routine performs one and only one operation (the examples he shows are sin(), getCustomerName(), eraseFile(), etc). So, exit() does 2 things. Therefore it shows Temporal cohesion (the operations are done in the same routine because they are done at the same time).
Now, the original arguments to exit($arg) was the return status of the applicaiton (See: linux exit status codes). In C, this is the integer value returned from main(). But since PHP doesn't have a native function, it was added in to the exit() parameter. Try it, add exit(2), and the return value of the program will be a status 2 (0 is usually success, see are there any standard linux exit status codes).
However, since PHP has a low barrier to entry, most developers likely won't know about status codes. So, it was made to accept a string. If the argument is a string, the status is echoed out on STDOUT, and then the application terminates. If it's an integer, it'll be returned from the program. So it's an artifact.
Now, is it a problem? Not really. Is it ideal? No. But it's also not horrible, since they are related. In the end, I wouldn't lose sleep over it...