goetas / xsd2php

Convert XSD into PHP classes and serialize into XML (deserialize too)
149 stars 62 forks source link

Suggestion: Base Class Extending #94

Open Garethp opened 9 years ago

Garethp commented 9 years ago

One of the modifications I made in my code, that I thought might be a good idea in general, was a way to have a base class for all items. So I put this at the top of ClassGenerator::generate()

        if (!($extends = $type->getExtends()) && class_exists($type->getNamespace())) {
            $extendNamespace = $type->getNamespace();
            $extendNamespace = explode('\\', $extendNamespace);
            $extendClass = array_pop($extendNamespace);
            $extendNamespace = implode('\\', $extendNamespace);

            $extends = new PHPClass();
            $extends->setName($extendClass);
            $extends->setNamespace($extendNamespace);

            $class->setExtendedClass($extends);
        }

So basically if I'm putting all of my elements in the \Some\Namespace\Types\ namespace, and the class \Some\Namespace\Type exists, then all classes generated should extend from that in one way or another. I don't know if you want to incorporate this idea in some way, but I found it a nice thing for myself

goetas commented 8 years ago

The main idea is to do not have special custom behavior coded directly into the project, probably a "custom plugin" here will be the best. See the idea expressed here https://github.com/goetas/xsd2php/issues/100#issuecomment-172182214