Autocomplete for PHP Objects with classes in PDT/Netbeans? - php

When I define an object of a class using new like this
$blah = new Whatever();
I get autocomplete for $blah. But how do I do it when I have $blah as a function parameter? Without autocomplete I am incomplete.
Edit: How do I do it if it's in an include and PDT or Netbeans can't figure it out? Is there any way to declare types for variables in PHP?

Method in first comment is called "type hinting", but you should use that wisely. Better solution is phpDoc.
/**
* Some description of function behaviour.
*
* #param Whatever $blah
*/
public function myFunction($blah)
{
$blah->
// Now $blah is Whatever object, autocompletion will work.
}
You can also use an inline phpDoc comment which does exactly the same thing.
public function myFunction($blah)
{
/* #var $blah Whatever */
$blah->
// Now $blah is Whatever object, autocompletion will work.
}

Try to pass parameter class definition into the function:
function myFunction(Whatever $blah) {
}

Related

OOP PHP: Calling classes as functions after creating an instance of them

What I would like to do after creating an instance of a class is to be able to call the name of that instance as a function. For example, consider the following class Foo:
$bar = new Foo(5); // generates 5 random ints between 0-100
bar(3); // get the third int in the object bar
Is this even possible in PHP or would it involve messing with the parser? Thanks in advance!
What this question is really about is creating a PHP functor and here's an example I lifted from here:
<?php
class SquareCallback
{
public function __invoke($value)
{
return $value * $value;
}
}
$squareObject = new SquareCallback;
var_dump($squareObject(3));

What does the function parameter without a dollar sign mean?

I've been studying PHP OOP and I came across this interesting function declaration:
class ShopProductWriter {
public $products = array();
public function addProduct(ShopProduct $shopProduct) {
//code here..
}
}
notice how the first parameter of the addProduct function ShopProduct doesn't have a dollar sign? What does it mean?
it is not a parameter. it is the type definition of $shopProduct. this function takes only 1 parameter so $shopProduct can only be a ShopProduct class
It's typehinting.
A parameter-list has to be comma-separated. This stands in front of a parameter and declares the parameter's type.
Your example typehint is for an object of the class ShopProduct.
Meaning: If something else was passed, like an integer or an array, the method would throw an exception. (It reflects inheritance. Assume there was an instance of SpecificShopProduct, a class that would be extending ShopProduct, you could pass that as well.)
It is possible to typehint objects and an array, an Class or an Interface. It is not possible to typehint normal variables types like integers or a string, and you cannot typehint an array of objects.
public function foo(array someArray, LoggerInterface $logger)
{
}
Yet for some of the other types you could use phpDoc comments, yet those are for readability and IDE support, they would not enforce a type; meaning you would have to take to check for yourself that a correct type was passed.
/**
* #param ShopProductWriter[] arrayOfObjects
* #param int $someInt
* #param string $someString
*/
public function foo(array arrayOfObjects, $someInt, $someString)
{
}

PHP combine $this variable

How to combine two variables to obtain / create new variable?
public $show_diary = 'my';
private my_diary(){
return 1;
}
public view_diary(){
return ${"this->"}.$this->show_diary.{"_diary()"}; // 1
return $this->.{"$this->show_diary"}._diary() // 2
}
both return nothing.
Your class should be like following:
class Test
{
public $show_diary;
function __construct()
{
$this->show_diary = "my";
}
private function my_diary(){
return 707;
}
public function view_diary(){
echo $this->{$this->show_diary."_diary"}(); // 707
}
}
It almost looks from your question like you are asking about how to turn simple variables into objects and then how to have one object contain another one. I could be way off, but I hope not:
So, first off, what is the differnce between an object and a simple variable? An object is really a collection of (generally) at least one property, which is sort of like a variable within it, and very often functions which do things to the properties of the object. Basically an object is like a complex variable.
In PHP, we need to first declare the strucutre of the object, this is done via a class statement, where we basicaly put the skeleton of what the object will be into place. This is done by the class statement. However, at this point, it hasn't actually been created, it is just like a plan for it when it is created later.
The creation is done via a command like:
$someVariable= new diary();
This executes so create a new variable, and lays it out with the structure, properties and functions defined in the class statement.
From then on, you can access various properties or call functions within it.
class show_diary
{
public $owner;
public function __construct()
{
$this->owner='My';
}
}
class view_diary
{
public $owner;
public $foo;
public function __construct()
{
$this->foo='bar';
$this->owner=new show_diary();
}
}
$diary= new view_diary();
print_r($diary);
The code gives us two classes. One of the classes has an instance of the other class within it.
I have used constructors, which are a special type of function that is executed each time we create a new instance of a class - basically each time we declare a variable of that type, the __construct function is called.
When the $diary= new view_diary(); code is called, it creates an instance of the view_diary class, and in doing so, the first thing it does is assigns it's own foo property to have the value 'bar' in it. Then, it sets it's owner property to be an instance of show_diary which in turn then kicks off the __construct function within the new instance. That in turn assigns the owner property of the child item to have the value 'My'.
If you want to access single properties of the object, you can do so by the following syntax:
echo $diary->foo;
To access a property of an object inside the object, you simply add more arrows:
echo $diary->owner->owner;
Like this?
$diary = $this->show_diary . '_diary';
return $this->$diary();

Can I use PHP reflection to extract function code?

I am using PHP ReflectionClass to extract as much information about a class as possible. Can I also use this to get return values from functions? Or does that not make sense since reflection only profiles what an object accepts?
You can not rely, if a function has a well defined return value you can simply extract from the source code. Imagine something like this:
return $this->isValid() ? $result : $this->createNullObject();
Thats hard (/impossible) to parse just to get the return value. You can use DocComments instead. #return is the usual tag for that use
/**
* MyMethod
*
* #return int
*/
Call getDocComment() on a ReflectionMethod-object and then parse the docComment.
for internal functions you could use
$reflect = new ReflectionExtension('standard');
echo "<pre>" . $reflect . "</pre>";

fetch php method comments

I want to fetch a method's comments,take below method for example:
/**
* Returns the regex to extract all inputs from a file.
* #param string The class name to search for.
* #return string The regex.
*/
public function method($param)
{
//...
}
the result should be
Returns the regex to extract all inputs from a file.
#param string The class name to search for.
#return string The regex.
the way I find is use a function like file_get_content to get file content -> filter the method I want -> fetch the comment use regexp
it seems a bit complicated , is there any convenient way to archive this?
actually you can get a method's doc comments with getDocComment
$ref=new ReflectionMethod('className', 'methodName');
echo $ref->getDocComment();
If you want to use the comment in PHP for something check out getDocComment in php's reflection api
PHP Doc. Like Java Doc.
For a method dump I use this little function I composed.
It fetches all methods from provided class that are public(and thus of use to you).
I personally use a dump() method to nicely format the outputted array of method names and descriptions, but that's not needed if you wish to use it for something else :-)
function getDocumentation($inspectclass) {
/** Get a list of all methods */
$methods = get_class_methods($inspectclass);
/** Get the class name */
$class =get_class($inspectclass);
$arr = [];
foreach($methods as $method) {
$ref=new ReflectionMethod( $class, $method);
/** No use getting private methods */
if($ref->isPublic()) {
$arr[$method] = $ref->getDocComment();
}
}
/** dump is a formatting function I use, feel free to use your own */
return dump($arr);
}
echo getDocumentation($this);

Categories