Today I have been refactoring code which I wrote half an year ago and I made this mistake:
I wrote
$url = $someUrl - $file->getName();
instead of
$url = $someUrl . $file->getName();
Suddenly the other code stopped working. However not for subtraction of strings but for something like fopen($url, "w") where $url was invalid. Of course. But question is what is really stored in $url?
In my case there was "/folder/subfolder" in $someUrl and "something.txt" in $file->getName(). The result in $url was ... tamtadadaa ... 0. Ok, when you think about it, it is simple. PHP takes both strings and convert them to numbers and those subtracts. Fine. As I said in my case the result was 0. That is because none of both strings starts with number. In case that the first string was "12 angry men" the result would be "12".
Other example, first string is "12 angry men" and second is "1 angry bird". The result is 11.
There are more similar things in php and you should know them or you'll be surprised one day. For example try echo 0 == "Hello world"
Monday, October 31, 2011
Tuesday, April 12, 2011
Reference or new object
I was writing some function and I needed to return object. Actually that function just take object as parameter, drag out attributes, do some work and set new attribute to object and return it. When I am doing this I always consider if I should return the same object or create new one. The point is that when I return the same object, it may lead to confusing mistakes. Consider the piece of pseudo-code below:
We have records in database and we store them as name part which look like "prefix-name" and data part. User puts the name and our dummy management add the prefix part. If we are dummy too and do this as object reference we are lost.
class DummyObject {
public $name;
public $data;
}
class DatabaseManager {
public function load(DummyObject $object){
//Retrieve data from database and set them.
$this->makeRightPath($object);
$object->data = "Data with long story to tell";
return $object;
}
//This sets the correct name of record in DB
private function makeRightPath(DummyObject $object) {
$object->name = "prefix-" . $object->name;
return $object;
}
}
$object = new DummyObject();
$object->name = "DummyName";
$mng = new DatabaseManager();
$loaded = $mng->load($object);
echo $loaded->name;
The result of echo will be "prefix-DummyName" which user obviously does not want to see. In addition if we want to load (or save etc.) the same object again it leads to loading with name "prefix-prefix-DummyName" which is actually not in DB.
I know this is not the best example of what I wanted to say (it is my first example ever) but it shows you how important is thinking about references in function calling.
We have records in database and we store them as name part which look like "prefix-name" and data part. User puts the name and our dummy management add the prefix part. If we are dummy too and do this as object reference we are lost.
class DummyObject {
public $name;
public $data;
}
class DatabaseManager {
public function load(DummyObject $object){
//Retrieve data from database and set them.
$this->makeRightPath($object);
$object->data = "Data with long story to tell";
return $object;
}
//This sets the correct name of record in DB
private function makeRightPath(DummyObject $object) {
$object->name = "prefix-" . $object->name;
return $object;
}
}
$object = new DummyObject();
$object->name = "DummyName";
$mng = new DatabaseManager();
$loaded = $mng->load($object);
echo $loaded->name;
The result of echo will be "prefix-DummyName" which user obviously does not want to see. In addition if we want to load (or save etc.) the same object again it leads to loading with name "prefix-prefix-DummyName" which is actually not in DB.
I know this is not the best example of what I wanted to say (it is my first example ever) but it shows you how important is thinking about references in function calling.
Monday, March 28, 2011
Troubles with XML in JDK7 and changing to JDK6
Today I wanted to write some xml document which would be similar to some other xml in PHP project. So I opened the xml document and my NetBeans7Beta froze and I had to kill them through terminal. I tried several ways to force my NetBeans to open that document.
After three or four time I had to kill them I tried to switch JDK. I had been using JDK7 so I wanted to change it to JDK6. I didn't find a way to do that inside NB IDE so I tried to google it. Many thanks to this article.
I changed the line in NetBeans/etc/netbeans.conf to netbeans_jdkhome="path_to_java" (in my case path_to_java is /usr/lib/jvm/java-6-sun-1.6.0.24" - if you install java from repository, your path might be same).
I don't actually know if the JDK7 was the cause of my problem but it seems to be ok after changing to JDK6.
After three or four time I had to kill them I tried to switch JDK. I had been using JDK7 so I wanted to change it to JDK6. I didn't find a way to do that inside NB IDE so I tried to google it. Many thanks to this article.
I changed the line in NetBeans/etc/netbeans.conf to netbeans_jdkhome="path_to_java" (in my case path_to_java is /usr/lib/jvm/java-6-sun-1.6.0.24" - if you install java from repository, your path might be same).
I don't actually know if the JDK7 was the cause of my problem but it seems to be ok after changing to JDK6.
First post
This is my first post to blog in my whole life. I have never use blogs until now. But world is changing and so do my opinions to life and so on. Well for now I just have to say welcome and excuse my english
Subscribe to:
Comments (Atom)