Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Partial class in PHP like we have in C#

Tags:

Is there any simple way to have a concept of partial class in PHP like we have in C#? I've tried to make it possible using different namespace but it couldn't work.

like image 670
Rishi Vedpathak Avatar asked Feb 09 '14 09:02

Rishi Vedpathak


People also ask

What is partial class in PHP?

Partial class is a single class defined in multiple files. Trait: definitions that can be used in different classes.

Does C++ have partial class?

There can be zero or more partial class definitions for every full definition of a class. Every partial class definition of a class must lexically precede the one full definition of that class, but doesn't have to precede forward declarations of the class.

What is partial class in c3?

The partial keyword indicates that other parts of the class, struct, or interface can be defined in the namespace. All the parts must use the partial keyword. All the parts must be available at compile time to form the final type. All the parts must have the same accessibility, such as public , private , and so on.

Can we have partial classes in Javascript?

partial classes can work by convention. For example consider this convention. // file main function SomeObject() { for (var i = 0, ii = SomeObject. Partial.


2 Answers

They don't exist.

If, however, you're trying to implement a code generator for which user-land code could be attached (following the same use-case as in C#) then the following may be a viable option:

class Generator
{
    public function generate(Definition $definition)
    {
        if ($this->shouldGenerateTraitFor($definition)) {
            $this->generateTraitFor($definition);
        }
        $this->generateClassFor($definition);
    }
}

Given some implementation like the above, you could then:

(new Generator())->generate(new Definition([
    'class' => 'GeneratedClass', 
    'trait' => 'GeneratedTrait',
]));

And the resulting code may resemble:

class GeneratedClass
{
    use GeneratedTrait;
}

trait GeneratedTrait
{
    // @todo; add "partial" code
}

What is important to note about Generator::shouldGenerateTraitFor is that if it returns false, the trait will not be regenerated. This could be conditional on whether GeneratedTrait.php exists, and is necessary to ensure that when the class is regenerated the hand-written trait code isn't clobbered.

However, it could* be much to your benefit to consider object composition over this approach.

* There are times when I feel that the generated code approach can be cleaner, such as with "entity" types, but that's case-by-case.

like image 64
Dan Lugg Avatar answered Oct 14 '22 20:10

Dan Lugg


PHP uses Traits for this task:

http://php.net/manual/en/language.oop5.traits.php

They allow you to include class parts from an insulated partial file (a Trait) to different classes that shares its logic and maybe other shared logics (like multiple inheritance)

like image 42
Luca C. Avatar answered Oct 14 '22 21:10

Luca C.