Documenting PHP multiple inheritance with PhpDoc - php

I have multiple inheritance like this one: Can I extend a class using more than 1 class in PHP? (let's not discuss this approach itself please) and want my IDE to know about inherited class methods and properties. Is there a way to do it with PhpDoc?

It seems there is currently no way to do it easily. I've created a ticket at PhpStorm issue tracker. Maybe they will add support for this feature.
http://youtrack.jetbrains.net/issue/WI-1730

The #method anotation should be used for classes implementing __call. On a related note, for __get, __set and __isset, the #property annotations should be used. The only thing I don't know for sure is whether Eclipse PDT supports these annotations. I know NetBeans does.

there is no support for multiple inheritances at class level. This means you can't extend more than one class at a time. However multiple inheritance is supported in interfaces. An interface can extend an arbitrary number of other interfaces
at a time.

Related

Why we use abstract class and interface ? Difference between Class, Abstarct Class and Interface

I know this is very repetitive question i also read most of the post but not found the satisfactory answers. I know the bookish difference which i read from most of the posts.
Can any one please tell me
why we use abstract class and interface?
when we use this one?
i know that when we have some repeated task but implementing in a different way each time then we use abstract class.
i think we can implement this by a normal class and subclass using overriding then why we use abstract? also their is a difference of access specifier between abstract class and interface as well as the compulsion of implementation in interface.
Interface is quite acceptable that when we want to force to implement all method in that situation we use interface but why abstract class? Is compulsion of implementation is just a difference?
thanks in advance.
Beautifully explained here - https://www.codeproject.com/Articles/11155/Abstract-Class-versus-Interface
In a nutshell,
Interfaces are used to define the peripheral abilities of a class. In other words both Human and Vehicle can inherit from a IMovable interface.
An abstract class defines the core identity of a class and there it is used for objects of the same type.
Also take a look at https://msdn.microsoft.com/en-us/library/scsyfw1d(v=vs.71).aspx
Here are some recommendations to help you to decide whether to use an interface or an abstract class to provide polymorphism for your components.
If you anticipate creating multiple versions of your component, create an abstract class. Abstract classes provide a simple and easy way to version your components. By updating the base class, all inheriting classes are automatically updated with the change. Interfaces, on the other hand, cannot be changed once created. If a new version of an interface is required, you must create a whole new interface.
If the functionality you are creating will be useful across a wide range of disparate objects, use an interface. Abstract classes should be used primarily for objects that are closely related, whereas interfaces are best suited for providing common functionality to unrelated classes.
If you are designing small, concise bits of functionality, use interfaces. If you are designing large functional units, use an abstract class.
If you want to provide common, implemented functionality among all implementations of your component, use an abstract class. Abstract classes allow you to partially implement your class, whereas interfaces contain no implementation for any members.
Consider this.
An interface has no implementation. It forces prototypes onto a class but does not help implementing them.
An interface is not tied to a class hierarchy. You can apply the same interface to multiple, entirely different class trees. This is important and one of the things that an interface offers over an abstract class. An interface is implementation independent, you can "slap it onto" any class. This is the most powerful aspect of an interface.

PHP protected java style

I have a functionality that need to be shared by a few classes that manage a common aspect of my software. In Java, i would have all theses classes in the same package and the common functionality would be in a protected method in a helper class.
In PHP, protected method mean that you can only use it in sub-classes so my current solution is to make the method protected and have all the classes needing this method to extend the helper class. The problem with my current solution is the fact than you can't inherit multiple classes so lets say i need to helper classes, im ...
So, is there a way to have a method visibility comparable to java protected in PHP? If not, any cleaner way to solve my problem?
It sounds that you are looking to use traits.
http://php.net/manual/en/language.oop5.traits.php
This would let you define your shared protected method and provide to the classes that require it. I would recommend implementing this with an interface so that you can specify that the method provided by the trait has to be there.
PHP only lets you extend one abstract class however you can implement multiple interfaces.
You could try namespaces (5.3) and reference the utility class instead of extending it. This doesn't quite solve the problem but it does help isolate your class. I try to avoid using private methods.
In PHP 5.4 they added traits which kind of allow a class to extend multiple classes. Schleis has already answered with that.

Real multiple class inheritance, not a collection of methods via traits

I understand the way you extend a class like ChildClass extends ParentClass, I understand use of class abstraction and interfaces, and I do understand traits
In the articles I have read most of people stated that traits is used in practice to achieve multiple inheritance that is not done by extends functionality as it supports only single extension.
As I understand traits it is more of a collection of functions that can be used by different classes or other traits.
In this question I ask you to tell me how is it correctly done to literally do extension of more than one class not adding some function-collection.
I can be extremely clear about this:
PHP does not allow this.
I indeed believe that traits provides a way to achieve multiple inheritance, under some restricted conditions. If your codebase is extremely OOP pure and well designed, traits may allow you to achieve multiple inheritence.
But it's not a catch all. If this is a stumbling block for you, don't blame this on PHP's design, you probably need to think about a different way to do what you want. (for example: delegate instead of sub-class).

What Are the Reflection Classes in php? [duplicate]

This question already has an answer here:
Closed 11 years ago.
Possible Duplicate:
PHP: The Reflection API - Great Addition To PHP With Little Use
Finding documentation is one problem, but the quality of documentation is another, equally important, problem. Most developers know the value of accurately commented code, but when i am in the middle of coding, the meaning of your code always seems crystal clear, so comments appear superfluous. Besides, there’s always the ultimate excuse for the absence of internal documentation—i want to keep file size small to reduce download time.
This is often the situation with internal documentation, but external documentation fares no better. It doesn’t make sense to write it as i go because things always change, but, by the time i’ve finished coding, documentation is the furthest thing from my mind.
However: I searched in google and I found a solution called Reflection Classes Could any one describe it briefly plz ?
Excerpt from the book Object-oriented PHP: concepts, techniques, and code
By Peter Lavin:
This group of classes was created for the express purpose of introspecting other classes. These classes
make it possible to examine the properties of other classes by retrieving metadata
about classes; you can even use them to examine the reflection classes themselves.
Reflection provides information about the modifiers of a class or interface—whether that class is final or static, for example.
It can also reveal all the methods and data members of a class and all the modifiers applied to them.
Parameters passed to methods can also be introspected and the names of variables exposed. Through reflection it is possible to automate the documentation of built-in classes or user-defined
classes.
It turns out that the central repository of information about classes was right in front of us all the time. PHP can tell us all about itself through the mirror of the reflection classes.
The reflection group of classes or Application Programming Interface (API) is made up of a number of different classes and one
interface, shown here:
class Reflection
interface Reflector
class ReflectionException extends Exception
class ReflectionFunction implements Reflector
class ReflectionParameter implements Reflector
class ReflectionMethod extends ReflectionFunction
class ReflectionClass implements Reflector
class ReflectionObject extends ReflectionClass
class ReflectionProperty implements Reflector
class ReflectionExtension implements Reflector
Look at this list, there is actually no class ancestor common to all reflection classes. On the other hand, the Reflector interface is shared by all classes except Reflection and ReflectionException.
ReflectionMethod extendsReflectionFunction, ReflectionObject extends ReflectionClass, and ReflectionException extends Exception.
ReflectionObject shares all the methods of ReflectionClass; the only difference between these classes is that ReflectionObject takes a class instance rather than a class name as a parameter—using an instance, you can introspect a class without knowing anything about it, even its name.
Reflection usually refers to accessing the definition of types and functions during runtime. It is most useful in static languages such as C# or Java, but I guess it could be useful in dynamic languages such as PHP too.
In PHP, it seems it can be used to retrieve documentation of existing classes/functions. But I don't think it will help you with writing documentation.
Reflection explained in general is done on the Wikipedia page:
In computer science, reflection is the process by which a computer program can observe (do type introspection) and modify its own structure and behavior at runtime.
(Excerpt)
The concrete implementation in PHP is documented in the book Reflection (Introduction) PHP Manual:
PHP 5 comes with a complete reflection API that adds the ability to reverse-engineer classes, interfaces, functions, methods and extensions. Additionally, the reflection API offers ways to retrieve doc comments for functions, classes and methods.
(Excerpt)
The reflection class is used to get information about the current state of the application. It's called reflection, because it looks at it's self, and can tell you information about the program your running, at run time.

PHP: Why isn't it possible to extend more than one class?

I've faced a situation when I want to extend two parent classes, but php does not allow this.
Why I can't extend more than one class, but can implement more than one interface. What's wrong with extending many classes?
It seemed to me like a pretty obvious thing, until I got parse errors.
Is it a bad practice? If so, what are the alternatives?
Is it possible in other languages?
Why multiple inheritance is forbidden in some/most programming languages is argued with the diamond problem http://en.wikipedia.org/wiki/Diamond_problem.
Put simple if you have a car that can swim and drive because it inherits from vehicle and boat what happens on execution of the move function!?
Try using interfaces and follow the Strategy pattern or State pattern.
You're probably looking for: Multiple Inheritance in PHP.
It seems to be possible in Python.
Take a look at Can I extend a class using more than 1 class in PHP?
Is it a bad practice? If so, what are alternatives?
Unless the language is specifically designed for it, yes. Consider, you have two classes, A and B. Both classes provide a public method foo() which have identical signatures (not hard in PHP). Now, you make a class C which extends both A and B.
Now, you call C.foo(). Without explicit instructions, how does the interpreter know which version of foo() to call?
It's not supported by PHP. It can however be simulated using runkit, APD or by just overriding __call and __get to simulate inheritance from multiple classes. Symfony (and I seldomly recommand that) also provides "sfMixin" or "sfMixer" for multiple inheritance.
Separate classes implementing the same method is not a good argument against multiple inheritance, as currently multiple interfaces can be implemented. You just can't implement two interfaces with the same method. Quote from http://www.php.net/manual/en/language.oop5.interfaces.php: "A class cannot implement two interfaces that share function names, since it would cause ambiguity."
I know this question is 2 years old but I've only just had the same problem. My work-around is this: if you have one regular class and two abstracts and want to extend both, e.g abstract class AbstractOne and abstract class AbstractTwo, you can say:
abstract class AbstractOne extends AbstractTwo {
}
Then add to the main class like this:
class MyMainClass extends AbstractOne {
}
This way, it inherits both AbstractOne and AbstractTwo.

Categories