Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static and Non-Static Calling in PHP

Tags:

oop

php

ok I have this code, that I'm studying

 class scope{

    function printme(){
        return "hello";
    }

    public static function printme(){
        return "hello"; 
    }

 }

$s = new scope();
echo $s->printme(); //non-static call
echo "<br>";
echo scope::printme(); //static call

Now, this is not really the code of my project but these are the things I want to do

  1. I want to create a class the will contain static and non-static functions.
  2. I want a function to be available both on static and non-static calls.

As non-static function has a lot of operations on it, I also need to call it as a static function so that I will not need to instantiate the class. Is this possible? or I really needed to rewrite the function to another function or class?

NOTE: tell me if I'm doing some bad programming already.

like image 302
Netorica Avatar asked Jun 06 '12 03:06

Netorica


People also ask

What is static and non static in PHP?

Static class contains static variables and static methods whereas instantiated class contains non-static variables and non-static methods. Programs having static classes are hard to test and to extend while programs with non-static classes provide easy testing and extending property.

Can I call non static method from static method PHP?

In PHP 7, calling non-static methods statically is deprecated, and will generate an E_DEPRECATED warning. See Static methods (php.net) for details. In the following example, the method foo() is called as dynamic while actually it is static.

What is the difference between static and non static fields?

A static variable acts as a global variable and is shared among all the objects of the class. A non-static variables are specific to instance object in which they are created. Static variables occupies less space and memory allocation happens once. A non-static variable may occupy more space.

What is static in PHP?

The static keyword is used to declare properties and methods of a class as static. Static properties and methods can be used without creating an instance of the class. The static keyword is also used to declare variables in a function which keep their value after the function has ended.


2 Answers

Here is the rule:

A static method can be used in both static method and non-static method.

A non-static method can only be used in a non-static method.

like image 195
xdazz Avatar answered Oct 17 '22 11:10

xdazz


If the instance of your class is rarely needed, you can have the static method create an instance, call the non-static method and return the value.

class Scope {
    public function mynonstatic() {
    }

    public static function mystatic() {
        $s = new Scope();
        return $s->mynonstatic();
    }
}

Remember that a static method is really just a global function with reduced scope. They are useful, but are should not be created without good reason.

like image 38
walrii Avatar answered Oct 17 '22 10:10

walrii