Call variable inside class from function inside class without $this - php

I recently needed to change my function to static and now $this does not work, and i need to get variable in the same class is there way to do it without $this?

Prefix it with:
self::$VariableName
or
MyClassName::$VariableName
(For more info: http://php.net/manual/en/language.oop5.static.php)

Static functions, by definition, are never called on an instantiated object, so $this is meaningless in that context (doesn't point to the current object).

In a static class, you could access it by self::

Related

using functions inside constructors php

In an effort to keep my code clean, I am attempting to replace a whole bunch of code in my constructor with a function. I believe I am calling the function correctly but i'm not able to assign values to the variables as intended.
public function __construct($docID) {
self::getDocumentInfo($docID);
self::getTranscriptionInfo($docID);
}
private static function getTranscriptionInfo($docID) {
$this->documentTranscription = 5;
}
Im getting an error "PHP Fatal error: Using $this when not in object context in ...". This is simplified for postings purpose, but would it be better just have a very large constructor and skip the functions all together? Or is their a better way to assign values?
A static method is not a part of the class instance. The static keyword means the method can be called within your class but it won't have any of the instance variables.
Remove the static keyword and change this:
self::getDocumentInfo($docID);
self::getTranscriptionInfo($docID);
to this:
$this->getDocumentInfo($docID);
$this->getTranscriptionInfo($docID);
Using $this means it will be calling it within the right instance context.
Some info the static keyword (added emphasis in italics):
Declaring class properties or methods as static makes them accessible without needing an instantiation of the class. A property declared as static cannot be accessed with an instantiated class object (though a static method can).
Because static methods are callable without an instance of the object created, the pseudo-variable $this is not available inside the method declared as static.
Source
http://php.net/manual/en/language.oop5.static.php

Accessing static methods using newly created $obj or using class Name

See the class definition below:
I am currently using 5.3.9 version of PHP
class A{
static function ab(){
echo "static function ab<br>";
}
public function xy(){
echo "public function xy<br>";
}
}
$obj = new A();
$obj->ab();
A::ab();
Both functions call give the same output without any error
static function ab
static function ab
How it is possible that static method can also be called by class object?
Because static method only calls by using class name only?!
Now what is the difference between accessing these two ways to call static method?
Referring to php.net website
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).
A big difference is
Because static methods are callable without an instance of the object created, the pseudo-variable $this is not available inside the method declared as static.
Refer to the page php.net/manual/en/language.oop5.static.php for more details
As long as you are just echoing a simple string, there's no difference, if your method will be declared static or public, since static method can also be called with the object instance. As of PHP 5.5 an error will raise if you call your public method with a static way. However, the static method can be called with classname::staticMethod() so the page should only know about the class, but not really needs an instance of it.
The other deal is the method content. As I said, if you just echo a string, you don't need a static method for that. A static method is out of the object context. That means you cannot access properties or methods from the current object via $this

Calling a static method non statically

I came across something like this, and am not sure what to make off it. Is there any good reason to do this, or to avoid it?
class Foo {
static public function bar() {}
}
someMethod() {
$instanceOfFoo->bar();
}
The PHP documentation says:
[...] A property declared as static can not be accessed with an instantiated class object (though a static method can).
[...] Static properties cannot be accessed through the object using the arrow operator ->.
without specifying anything special for static methods being called by ->. You should definitely avoid it though, because it causes confusion to the reader who's expecting $obj->meth() to be a non-static method and Cls::meth() a static method.
Surprisingly this behavior is not triggering any error. The reason for this is that a static method, called by $object->method() is internally translated to className::method() at run time (with the only difference being that $this = NULL is set).
You can call the particular function as below.
Foo::bar();
You don't have to create an object to call a static function. Basically we write static functions to call the function without an instance of the class in which it's defined.
It's okay to call a static function with an object but why do so when you have a simpler and cleaner method.

Difference in Class Calling PHP

With learning fuelPHP, I am introduced on calling classes using scope resolution, or :: in sense. Typically, when we call a method in a class we do this ...
$myclass = new myclass();
$myclass->mymethod();
On fuel, methods are usually called in this manner ...
myclass::mymethod();
I was wondering if there are any difference between the two? Is the scope resolution is something of an update on 5.3 as well ... if not, which one is ideal, or when should I use these.
Thanks.
The scope resolution operator is used to access either class constants like ::const, static variables like ::$var or call static methods like ::method().
See http://php.net/manual/en/language.oop5.static.php
Static methods can be called without having an instance of the class they are defined in. They're defined in that class with the static keyword.
For example, one of CakePHP's static methods is defined like this:
class ClassRegistry {
// ...
public static function &getInstance() {
// ...
}
}
... which you can call like ClassRegistry::getInstance().
Without the static keyword, you'd need an instance of the ClassRegistry class to call that function.
You can read more here, especially about why using static methods in your own code can sometimes be a bad idea: http://kore-nordmann.de/blog/0103_static_considered_harmful.html
I am not sure how would myclass::mymethod(); work, since I use such syntax only when I am calling a STATIC class.
MyClass::DoSomething();
would call a static method named DoSomething()
while
$instance = new MyClass();
$instance->DoSomething();
would call the instance method.
I have not tested it but I believe you will run into an error if you do $instance::DoSomething()
I think the best way to understand why there is a static call and what it does behind the scene is to check this FuelPHP blog's entry: http://fuelphp.com/blog/2011/05/why-did-you-do-that
The obvious difference is that the first solution $myObject->myMethod() it's a dynamic call : you need an instance to execute myMethod().
In the second solution, MyClass::myMethod() is a static call. The class acts as a sort of namespace where a function belong. You don't need an instance for that.

Difference between "->" and "::" in PHP MySQLi OOP

Can anyone tell the difference between mysqli->commit and mysqli::commit?
The header in this page is mysqli::commit, but in examples they use mysqli->commit.
-> is used when referring to a member of an object.
:: is the Scope Resolution Operator and is used to refer to a static member of a Class.
Consider the following class:
class FooBar {
public static function fizz() {
echo "Fizz";
}
public function buzz() {
echo "Buzz";
}
}
You would call the function buzz() using ->:
$myFooBar = new FooBar();
$myFooBar->buzz();
But would use :: to call the functon fizz(), as it is a static member (a member which doesn't require an instance of the class to be called):
FooBar::fizz();
Also, while we are talking about the difference between static members versus instantiated members, you cannot use $this to refer to the current instance within static members. You use self instead (no leading $) which refers to the current class, or parent if you want to refer to the parent class, or if you have the pleasure of working with PHP 5.3.0, static (which allows for late static binding).
The documentation uses :: to refer to a function inside a class as the class name in the header is not an instance of the class. Still using the same example, a documentation entry referring to the function buzz() would use the following header:
FooBar::buzz
But unless the documentation specifies it's a static member, you will need to use -> on an instance to call it:
$myFooBar = new FooBar();
$myFooBar->buzz();
:: is used for static methods.
-> is used for method of an object if you already have the instance.
If you have an instance of an object, you use -> to refer to a method inside this instance:
$foo = new Foo();
$foo->bar();
Using :: calls a static method without having to create an instance of the object:
Foo::bar();
A static method cannot refer to an it's current instance through $this, but can refer to itself (current class) by using self.
:: specifies a static (class) method, which is callable without actually instantiating an object. -> specifies an instance (object) method, for which you need an object instantiated to be able to use.
So for example, if you had a variable $m which was an instance of class mysqli, you would call commit by saying $m->commit(), or you could call commit statically by saying MySQLi::commit()
:: accesses the class' function without instancing an object.
in mysqli->commit, mysqli is a instance of MySQLi
in mysqli::commit call a static method
mysqli->commit is a public function and mysqli::commit is a static function the two are php object notations of mysqli class.
usually in php.net documentation :: means that this class has that method. For pratical usage you must follow the example so use the -> sintax.
The -> operator is for object properties.
The :: operator is for class properties.

Categories