Showing posts with label php. Show all posts
Showing posts with label php. Show all posts

Wednesday, March 02, 2011

PHP 5.3 DirectoryIterator::getPathname bug on root

Using DirectoryIterator on root results in a different behavior of getPathname.

before 5.3 the output would be something like:
/var
/etc
...

after 5.3 it is:
//var
//etc
...
what the heck is it with the doubleslashes? It's only happening if you are using Root as the Directory to start with
new DirectoryIterator('/');
Although it seems to me like a bug, i found nothing on google. Reporting it was not possible as i am using a debian distributed version and i already know their answer. "Use the original one first"

Monday, September 24, 2007

PHP5 DomDocument schemaValidation problems

First i have to tell about a SOAP Connection to a webservice. I don't know if the developers ever thought about SOAP more than just a quick thought how can we transfer data, since from my end it looks as if they have absolute no idea what they were doing.

We have a Service that holds a bunch of personal data which we have to transfer to the webservice of an higher instance. Now you would think they provide a SOAP Connection where there are a lot of functions to send them the data for every person.. but no.. there are only two functions:
  • One which wants a XML String where all personal data has been collected together and
  • a second one which wants the same string as zip
So you see the whole point of the SOAP connection is just to send them the already prepared XML Files and then they work with the collected data somehow internal.

Fortunately they provided a xsd file so i was able to add additional validation to ensure the data is valid.

Now i created the XML Files using php5 DomDocument. The library makes it really easy to create Elements and it also holds a schemaValidation functionality.

I don't want to get into to much detail but when i said that they want all personal data of all people collected in one file you will notice that it would not be a good idea creating first all users in one file and then check if that file is ok... or perhaps i am just the only paranoid here, but i wanted to know exactly which person is making troubles.

So my code first creates the xml data for each person separately (although cached) and then i add the xml data to an prepared xml so i can validate against the schema.

The Problem
Now we get to the point. My Function first creates a Validation XML which helds all the markup and then adds the XML Data of the person using DomDocument->importNode and then appendChild at the correct position.

Unfortunately the schemaValidation finds errors where there are not really one, or so i thought. I got Error Messages which i was unable to see anything wrong. But i found a workaround.
Using DomDocument->loadXML(DomDocument->saveXML()) just before the validation i was able to sort of "normalize" the DomDocument and the schemaValidation worked.

I am not sure if the schemaValidation was just not designed for such a work or i did misjudge and used it completely wrong. But since there is not much documentation at php.net i had no other way.

Friday, January 20, 2006

PHP5 parent:: Object

Working with PHP5 getting you used to work with real Object Orientatet Classes. Extended Classes or using the parent:: Object in such an extended Class is nothing new.

But i did not know (till now) that it is possible to jump over an extended Class to that parent.

To explain what i mean we have this Example:

class foo {
protected $iFoo = 0;
protected function bar() {
$this->iFoo++;
echo "foo: {$this->iFoo}
";
}
}
class fooA extends foo {
protected function bar() {
$this->iFoo++;
echo 'fooA:';
parent::bar();
}
}
class fooB extends fooA {
public function bar() {
$this->iFoo++;
echo'fooB';
parent::bar();
}
}

$oFoo = new fooB();
$oFoo->bar();

In this example we have 3 Classes, each extended by the next. Normally calling $oFoo->bar(); will output: "fooBfooA:foo: 3" which indicates that it starts with the latest class fooB, then runs through fooA to foo.

But what if you need an extend on fooA, but you don't want to run through it, you just want to run through fooB->bar and then foo->bar, without fooA?

I thought there is no solution for it, but there is! If you just change the function in fooB, where it says: "parent::bar();" to "foo::bar();" it will jump directly to foo, without fooA and it is not static as you may first think!

The Result will display: "fooBfoo: 2"

Tuesday, November 22, 2005

PHP5 reading XML using DOM

Working with XML Files in php5 you definetly want to work with DOM. Using DOM is very easy, but unfortunately the documentation is not complete and there are one or two things missing, which stop your work for several hours.

We use XML for multiple languages on websites, so our content is in the XML File written in XHTML.
If you use XML Files for different Languages like for example german, you need specific characters. This made us some troubles, but in the end it's more than easy.

First you have to tell the xml File which language you are using in the "xml" header through using encoding. For Example a german File looks like this:
<?xml version="1.0" encoding="iso-8859-1">
Next if you are using HTML Entities like or ü you have to define a DOCTYPE. Since we used XHTML Entities we used a dtd from w3.org:
<!DOCTYPE shiva SYSTEM "http://www.w3.org/TR/xhtml1/DTD/xhtml-lat1.ent">
Now we get to the PHP Part:
Loading a XML File using DOM is very easy. First you have to create a new DOMDocument Object and then load the File:
$oXML = new DOMDocument();
$oXML->load(Filename with Path);
Now if you have a external DOCTYPE like we did, you have to enable a Variable or it will not work before the ->load(..) Function:
$oXML->resolveExternals = true;
This enables to read external DOCTYPE Files.
Remember: If you are just using a couple of Entities, it's quicker to just put them directly in the File or create your own File with just the entities you need and link to that File. There were some comments on php.net that enabling that Variable will slow down the load Procedure for some time.

Now the load Procedure will not send you any more Warning Messages and possible an Exception, but you will still miss the HTML Entities in your Object.
Instead of Hello World which should look like that: Hello World, you will get a HelloWorld. You are still missing the Final Variable:
$oXML->substituteEntities = true;
With that you will get your Hello World and all other Entities your DOCTYPE defines.

Addition:
We had the additional Problem that we used htmlentities in the text like "&amp;" .. but we needed it also in php this way and using a doctype from w3c converted our entity into &, so we wrote our own Doctype File where we made a little hack so we could use htmlentities.

To use "&uuml ;" for ü we wrote into the dtd File the following:
<!ENTITY uuml "&amp;uuml ;">