How to use Normal Variable inside Static Method - php

class Exam {
public $foo = 1;
public static function increaseFoo(){
$this->foo++;
echo $this->foo;
}
}
Exam::increaseFoo();
This code generate an Error
E_ERROR : type 1 -- Using $this when not in object context -- at line 5
Is that possible to use global variable into static mathod?

replace $this with self, also you must mark your variable as static when using it in a static method:
class Exam {
public static $foo = 1;
public static function increaseFoo(){
self::$foo++;
echo self::$foo;
}
}
Exam::increaseFoo();

Variable inside class has to be static. No need to declare the variable as public.
class Exam {
private static $foo = 1;
public static function increaseFoo(){
self::$foo++;
echo self::$foo;
}
}
Exam::increaseFoo();

Firstly, Variable needs to be static as the method where you want to use this variable is static method
Secondly, need to use self:: instead of $this-> while refferencing the class

Related

Static function of a class

I have a code here,
class someClass {
public $someMember;
public function __construct() {
$this->someMember = 1;
}
public static function getsomethingstatic() {
return $this->someMember * 5;
}
}
$obj = new someClass();
echo $obj::getsomethingstatic();
and return an error, I know it has something to do with static but I couldn't find good explanation. I know how to fix this, I'm just looking for an explanation which will add to my understanding.
Anyone?
A static function ($obj::) cannot return/use a non-static ($this) class property, you'd have to make getsomethingstatic non-static to return the variable or make the variable static and update your other functions respectively.
As $this refers to the instance in question and static functions by definition are used outside of the instance it is not possible to mix.
ProTip
In the future, please include the error in the OP. It was easy to spot the error in this question but it might not have been in another case so included the required information speeds up the process.
You don't use the object accessor -> within Static methods. Use the Scope-Resolution Operator :: instead; prefixing it with either self or static as shown below. However be sure to use only static member variables/properties within Static methods as well...
class someClass {
public static $someMember;
public function __construct() {
self::$someMember = 1;
// OR
static::$someMember = 1;
}
public static function getsomethingstatic() {
return self::$someMember * 5;
// OR
return static::$someMember * 5;
}
}
// TO CALL A STATIC METHOD OF A CLASS,
// YOU NEED NOT INSTANTIATE THE CLASS...
// SIMPLY CALL THE METHOD DIRECTLY ON THE CLASS ITSELF....
echo someClass::getsomethingstatic();

How to call a function or method inside its own class in php?

I declare my class in PHP and several functions inside. I need to call one of this functions inside another function but I got the error that this function is undefined. This is what I have:
<?php
class A{
function b($msg){
return $msg;
}
function c(){
$m = b('Mesage');
echo $m;
}
}
This is basic OOP. You use the $this keyword to refer to any properties and methods of the class:
<?php
class A{
function b($msg){
return $msg;
}
function c(){
$m = $this->b('Mesage');
echo $m;
}
}
I would recommend cleaning up this code and setting the visibility of your methods (e.e. private, protected, and public)
<?php
class A{
protected function b($msg){
return $msg;
}
public function c(){
$m = $this->b('Mesage');
echo $m;
}
}
You can use class functions using $this
<?php
class A{
function b($msg){
return $msg;
}
function c(){
$m = $this->b('Mesage');
echo $m;
}
}
You need to use $this to refer to the current object
$this-> inside of an object, or self:: in a static context (either for or from a static method).
To call functions within the current class you need to use $this, this keyword is used for non-static function members.
Also just a quick tip if the functions being called will only be used within the class set a visibility of private
private function myfunc(){}
If you going to call it from outside the class then leave it as it is. If you don't declare the visibility of a member function in PHP, it is public by default. It is good practice to set the visibility for any member function you write.
public function myfunc(){}
Or protected if this class is going to be inherited by another, and you want only the child class to have those functions.
protected function myfunc(){}

public static function php pass variable

I need to pass a variable from a static function to another within the same class.
I do not write the full code, I need the theoretical procedure
enter code here
class One
{
public static function One()
{
/**
* some code extract from DB $one
*/
}
public static function two()
{
/**
* I need to retrieve the variable $one to use it in another query DB
*/
}
}
Note:
you can't use $this in a static function
Declare $one as a static variable:
private static $one;
And you can access it using : self::$one
You need to declare your variable within your One class, then you can retrieve it using self and the scope resolution operator ::.
class One {
private static $one;
public static function One() {
self::$one = something_from_your_db();
}
public static function two() {
do_something(self::$one);
}
}

Use Class property inside of a method's function

I'm trying to use myVar inside my of a method's function. I have already tried adding global but still nothing. I know this is probably basic but I can't seem to find it.
class myClass{
public $myVar;
public function myFunction() {
function myInnerFunction() {
//how do I use this variable here
echo $this->myVar;
}
}
}
Whenever I try using $this I get this error: 'Using $this when not in object context in...'
You should use $this->myVar
See the PHP Documentation - The Basics
<?php
class SimpleClass
{
// property declaration
public $var = 'a default value';
// method declaration
public function displayVar() {
echo $this->var;
}
}
?>
The pseudo-variable $this is available when a method is called from
within an object context. $this is a reference to the calling object
(usually the object to which the method belongs
Update:
In your new code sample, myInnerFunction is a nested function and is not accessible until the myFunction method is called. Once the myFunction method is called, the myInnerFunction becomes part of the global scope.
Maybe this is what you are looking for:
class myClass{
public $myVar;
public function myFunction() {
}
function myInnerFunction() {
//how do I use this variable here
echo $this->myVar;
}
}
Inner functions like myInnerFunction are always global in scope, even if they are defined inside of a member function in a class. See this question for another similar example
So, to PHP, the following are (almost) equivalent:
class myClass{
public $myVar;
public function myFunction() {
function myInnerFunction() {
//how do I use this variable here
echo $this->myVar;
}
}
}
And
class myClass{
public $myVar;
public function myFunction() {
}
}
function myInnerFunction() {
//how do I use this variable here
echo $this->myVar;
}
Hopefully the second example illustrates why $this is not even in scope for myInnerFunction. The solution is simply to pass the variable as a parameter to the function.
Pass it as an argument to the inner function.
You can use ReflectionProperty:
$prop = new ReflectionProperty("SimpleClass", 'var');
Full example:
class myClass{
public $myVar;
public function myFunction() {
function myInnerFunction() {
//how do I use this variable here
$prop = new ReflectionProperty("SimpleClass", 'myVar');
}
}
}
The solution above is good when you need each instance to have an own value. If you need all instances to have a same you can use static:
class myClass
{
public static $myVar = "this is my var's value";
public function myClass() {
echo self::$myVar;
}
}
new myClass();
see here

Is it possible to get the value of an overridden NON static member variable of a parent class?

Is it possible to get the value of an overridden NON static member variable of a parent class?
I understand that to get the value of a STATIC member variable you use self::$var1 or ClassName::$var1, but how do you get the value of a NON static member variable?
For instance...
class One
{
public $var1 = 'old var';
}
class Two extends One
{
public $var1 = 'new var';
public function getOldVar()
{
//somehow get old var
}
}
Thanks so much in advance!
Nope. Once you've overridden a non-static property value it's gone. You can't use the parent:: syntax with non-static properties like you can with methods.
However, using the static keyword you can utilize PHP's late static binding capabilities to access a static parent property because the static values are bound to the class in which they're assigned:
class Top
{
public static $prop = 'Parent';
}
class Child extends Top {
public static $prop = 'Child';
public static function getParentProp() {
return parent::$prop;
}
public static function getProp() {
return static::$prop;
}
}
echo Child::getParentProp(); // outputs "Parent"
echo Child::getProp(); // outputs "Child"
Note that you cannot override a non-static property with a static one in a child class to achieve what you're attempting because PHP (and all other scripting languages, I believe) uses the same table to store property names. This is just a limitation of the language.
You can do this using reflection:
class One {
public $var1 = 'old var';
}
class Two extends One {
public $var1 = 'new var';
public function getOldVar() {
$ref = new ReflectionClass(get_parent_class());
$props = $ref->getDefaultProperties();
return $props['var1'];
}
}
$two = new Two;
var_dump($two->getOldVar()); // string(7) "old var"

Categories