Use $this to refer to the current object. Use self to refer to the current class. In other words, use $this->member for non-static members, use self::$member for static members.
Source: When to use self vs this -stackoverflow
Declaring class properties or methods as static makes them accessible without needing an instantiation of the class. A property declared as static can not be accessed with an instantiated class object (though a static method can).
Source: Static Class -PHP Manual
One thing about static methods/classes is that they don't facilitate unit testing (at least in PHP, but probably in other languages too). Another thing about static data is that only one instance of it exists in your program : if you set MyClass::$myData to some value somewhere, it'll have this value, and only it, everywhere -- Speaking about the user, you would be able to have only one user -- which is not that great, is it ?
Source: When to use static -stackoverflow
Anonymous functions, also known as closures, allow the creation of functions which have no specified name. They are most useful as the value of callback parameters, but they have many other uses.
Closures can also be used as the values of variables; PHP automatically converts such expressions into instances of the Closure internal class. Assigning a closure to a variable uses the same syntax as any other assignment, including the trailing semicolon :
Source: Anonymous functions -PHP Manual
- Public scope to make that variable/function available from anywhere, other classes and instances of the object.
- Private scope when you want your variable/function to be visible in its own class only.
- Protected scope when you want to make your variable/function visible in all classes that extend current class including the parent class.
Source: Public, Private, Protected -stackoverflow
Basically, instead of having your objects creating a dependency or asking a factory object to make one for them, you pass the needed dependencies in to the constructor, and you make it somebody else's problem (an object further up the dependency graph, or a dependency injector that builds the dependency graph). A dependency as I'm using it here is any other object the current object needs to hold a reference to.
Source: What is Dependency Injection? -stackoverflow
Checkout this great article on Dependency Injection.
Source: Dependency Injection Huh? -nettutsplus
Composition is an important concept in PHP. It occurs when an object creates another object; that is, the first object completely possesses the second object.
Source: Composition OOP
Assignment Operators -PHP Manual
Operators, PHP OPERATORS -w3schools
You could, naturally. However, if there are many objects that are of pretty much the same type, it might help to extract common functionality out into a "base" class, which means you don't have to duplicate that logic.
Source: Abstract class in php -stackoverflow
Resource - SQL JOINS -w3schools
/**
* FIZZ BUZZ
*
* Write a program that prints the numbers from 1 to 100. But for multiples of three
* print "Fizz" instead of the number and for the multiples of five print "Buzz".
* For numbers which are multiples of both three and five print “FizzBuzz”.
*
*
*/
for($i = 1; $i <= 100; $i ++)
{
if($i % 3 == 0 && $i % 5 == 0)
{
echo 'FizzBuzz';
}
else if($i % 3 == 0)
{
echo 'Fizz';
}
else if($i % 5 == 0)
{
echo 'Buzz';
}
else
{
echo $i;
}
echo '<br/>';
}
Other common questions include: