Jump to content

johnmerlino1

Members
  • Posts

    21
  • Joined

  • Last visited

johnmerlino1's Achievements

Member

Member (2/5)

0

Reputation

  1. I came across the PHP clone method. Does this clone first level fields and methods or does it also do deep copying e.g. copying references to those fields. For example, a field may be an array. Does it make a copy of the actual array or does it refer to the same array position in memory, so if the original changes, then the copy's changes as well?
  2. I'm reading over this book and I came across something that looked very odd: public function DisplayHeader() { ?> <table width="100%" cellpadding="12" cellspacing="0" border="0"> <tr bgcolor ="black"> <td align ="left"><img src = "logo.gif" /></td> <td> <h1>TLA Consulting Pty Ltd</h1> </td> <td align ="right"><img src = "logo.gif" /></td> </tr> </table> <?php } In the function it closes a php tag, uses raw html that is rendered as is on the page, and then returns back to opening the php tag. I understand why it is being done. It is easier to write raw html here than to echo it in a php block, but it just looks odd. Anyone actually use this technique?
  3. Do interfaces in PHP include default methods, which are available in Java 7 and 8. Default methods allow you to add default functionality in the interface itself. So when a class implements it, it can use the default definition from the interface. This is in contrast to an abstract method which has no method body.
  4. First, I read PHP does not support function overloading. Then later in the book in chapter of OOP, it states: So does it support function overloading or not? And of course my definition of function overloading is what you get in the Java programming language.
  5. So basically unset is the equivalent of setting an initialized value in Java back to null.
  6. In C, when you dynamically allocate memory, you have to free it, otherwise you face memory leaks: char *str; str = (char *) malloc(10); free(str); But in most other languages you do not need to worry about this. PHP has a function called unset, which unsets the variable's value. But why would you want to do this, if PHP has a garbage collector that will free variables once they leave scope anyway? I see no point of unset in a scripting language with garbage collection.
  7. In the below example, we match 0 or more alphanumeric characters in the beginning and 0 or more alphanumeric characters in the end. Then we create 4 groups. Each group has "?=" which means "the next text must be like this". Now in first group we match any character 8 or more times. The next group we match 0 or more characters and a digit. In the next group we match 0 or more characters and a lowercase letter. In the next group we match 0 or more characters and an uppercase letter. <?php $password = "Fyfjk34sdfjfsjq7"; if (preg_match("/^.*(?=.{8,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).*$/", $password)) { echo "Your passwords is strong."; } else { echo "Your password is weak."; } ?> My question is do these four groups impact each other? That is, the fact that the first group has 8 or more characters means that all the groups must have 8 or more characters. The fact that the second group has a digit means that all the groups must have a digit. Or do they work exclusively meaning that a 4 character word with a single digit would match this pattern, even though first group says it must have 8 characters.
  8. I find it a bit buggy that strpos returns false instead of -1. The equivalent C implementation returns -1: int strpos(char s[], char t[]) { int i, j, k; for (i = 0; s[i] != '\0'; i++) { for (j=i, k=0; t[k]!='\0' && s[j]==t[k]; j++, k++) ; if (k > 0 && t[k] == '\0') return i; } return -1; } The problem with returning false is that false can also evaluate to 0 which can also happen to be the first index of a match. Therefore, you have a hard to find bug. I know that you can use the identity operator to check for falseness: ===. But wouldn't it be a better design for the function to just return -1?
  9. Arrays in C are fixed length: #define MAX_SIZE 5 int main(void){ int n[ 5 ]; for(int i=0;i<MAX_SIZE;i++){ n[i]=i; } return 1; } If I try to insert an eleement into the array, it will result in undefined behavior. Java compensates for this by creating an actual object class called ArrayList or LinkedList, the former an internal store of array elements that dynamically resizes and the latter just a node tree with pointers to next and previous elements. ArrayList<Integer> arr = new ArrayList<Integer>(); arr.add(1); arr.add(2); It seens that the array() construct in PHP is not a function or a class but an internal construct. How does it dynamically resize arrays?
  10. Well, I linked the manual to the original question and that actually indeed was my question - why is this mode missing and since it is missing, is it not needed anymore in PHP5?
  11. I have the following code: $fp = fopen(“path_to_file”, ‘a’); flock($fp, LOCK_EX); fwrite($fp, $string); flock($fp, LOCK_UN); fclose($fp); If I try to lock the file in two different places at the same time, this will cause a race condition. How can I prevent this? I know in Java, for example, it has a concurrent library which contains reentrant lock, which basically tries to get the lock and if can't waits. What can I do in PHP?
  12. But if I want to open a file for reading, can I just use r or do I need to attach b as well? $fp = fopen(“$DOCUMENT_ROOT/../orders/orders.txt”, ‘rb’);
  13. I was looking at the manual: http://php.net/manual/en/function.fopen.php And I noticed the the binary mode 'b' is missing from the list. Any reason why?
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.