hi there,
I have one question
here is my code taken from the internet which is about the static function
but i dont exactly understand for what reason we use here static function and
what does mean self and code which is below
can anyone explain it to me

<?php
class Dynamic {
  static protected $methods = array();

  public static function registerMethod($method) {
    self::$methods[] = $method;
  }

  private function __call($method, $args) {
    if (in_array($method, self::$methods)) {
      return call_user_func_array($method, $args);
    }
  }
}

function test() {
  print "Hello World" . PHP_EOL;
}

Dynamic::registerMethod('test');
$d = new Dynamic();
$d->test();
?>

and i dont understand here
line 6 (self:: ), 10, 11 and the purpose of __call function
thanks beforehands

hi there,
I have one question
here is my code taken from the internet which is about the static function
but i dont exactly understand for what reason we use here static function and
what does mean self and code which is below
can anyone explain it to me

....

and i dont understand here
line 6 (self:: ), 10, 11 and the purpose of __call function
thanks beforehands

Static and dynamic class methods can be a bit hard to grasp at first. In order to understand them, you have to first understand the "class definition" and the objects created from the class definition.

The class definition, is the actual code you write to define a class. The Objects or Instances of that class definition, are copies of Objects created from the definition you wrote, and exist only in the memory of the program.

Thus when using the terms static and dynamic, you are referring to the static class definition, or the dynamic object created from that class.

Static properties and methods, are the properties and methods defined in the class, while dynamic properties and methods, are the properties and methods that the object creates from the definition of the class.

Note, that every property and method is essentially dynamic or static depending on how it is referenced. If it is referenced as a property of an object, it is dynamic, since the object is dynamic. If it is referenced as a property of the static class definition, then it is static.

However, PHP has to have you specifically define whether a property or method is static or dynamic in order to make the difference clearer.

A static function, is one that can be called without creating an object (instance) from the class.

For example, if you have the simple class:

class myclass {

}

You can create an instance or object from that class.

$myobject = new myclass;

Now you have an object, that is created from the definition of your "myclass" class. If you put a method in "myclass", eg:

class myclass {
  function mymethod() {

  }
}

Now when you create an object from "myclass" the object will have a method "mymethod" attached to it. You can now all "mymethod" two ways.

First by creating an object from "myclass", then calling the "mymethod" method attached to that object. This is called calling the method dynamically.

Eg:

// create an object from "myclass" class
$myobject = new myclass;
// call the "mymethod" method of $myobject
$myobject->mymethod();

The second way, is to call the method "mymethod" statically. This is when you do not create any object from your class, but instead, just call the method directly.

// call "mymethod" method of "myclass" class statically
myclass::mymethod();

The difference between the two, is that calling dynamically requires an object, while statically does not. Now you can generate many objects from one class, but you can only have one definition of a class.

eg:

// class definition (static)
class myclass {
  function mymethod() {

  }
}

// call "mymethod" of "myclass" definition statically
myclass::mymethod();

// create first object
$object1 = new myclass;
// call "mymethod" method of first object
$object1->mymethod();


// create second object
$object2 = new myclass;
// call "mymethod" method of second object
$object2->mymethod();

Note that each new object you create from your class is not related to the other. So any changes to $object1 will not affect $object2. They are separate copies or instances of the "myclass" class.

Ok, now if we put some properties in the class, we can show how dynamic methods access those properties, and how static methods access properties.

Just like dynamic and static methods, there are dynamic and static properties in a class.

Before PHP5, to define a static property you just put "static" in front of it.

eg:

class myclass {

  // static property
  static $my_static_property;

  function mymethod() {

  }
}

To reference the static property you use the following notation:

myclass::$my_static_property;

Note that this is similar to how we reference a static method, with the difference of the $ sign in front of the property name.

// call "mymethod" of "myclass" definition statically
myclass::mymethod();
// reference "$my_static_property" of "myclass" statically
myclass::$my_static_property;

Now lets assume you wanted to change the value of the static property "$my_static_property" of "myclass" class.

eg:

myclass::$my_static_property = 'changed';

In PHP, class definitions can be accessed from anywhere within your PHP script. This means that class definitions have a global scope. see: http://php.net/manual/en/language.variables.scope.php and http://php.net/manual/en/language.oop5.static.php

So if you change a static property of a class, then it is changed globally, since there is only one static property of that name.

However, the difference with dynamic properties, is that there can be many objects created from a class, and thus the dynamic properties are attached only to a certain object, changing them does not affect the other objects.

Eg:

// class definition (static)
class myclass {

  // static property
  static $my_static_property;

  // dynamic property
  var $my_dynamic_property;

  function mymethod() {
    
  }
}

$object1 = new myclass;
// change the "my_dynamic_property" of $object1
$object1->my_dynamic_property = 'changed';

$object2 = new myclass;
// "my_dynamic_property" property of $object2 is not affected

echo $object1->my_dynamic_property; // will not output anything

Now back to static and dynamic methods.

In a static method, when you want to refer to the class that the static method belongs to, you use the "self" keyword.

In a dynamic method you use the "$this" keyword.

eg:

// class definition (static)
class myclass {

  // static property
  static $my_static_property;

  // dynamic property
  var $my_dynamic_property;

  function mymethod() {
    self::my_static_property; // refers to static property "my_static_property"
    $this->my_dynamic_property; // refers to dynamic property "$my_dynamic_property"
  }
}

Now, as you can see, the method "mymethod" has references to both static and dynamic properties. Another main difference tween static and dynamic methods is that static methods cannot have $this references in them, since it is not dynamic. However, a dynamic method can have "self" references, since "self" still refers to the static definition of itself.

That was a bit long, hope it cleared some things up for you though.

thank you very much
but i konw what is class and what is private, public, protected methods. i also how class created and how and where we must use
but i want only learn for what reason we use static functions
can you understand me on my example which i have posted(if possibel)
Thank you for attention

hi there,
I have one question
here is my code taken from the internet which is about the static function
but i dont exactly understand for what reason we use here static function and
what does mean self and code which is below
can anyone explain it to me

<?php
class Dynamic {
  static protected $methods = array();

  public static function registerMethod($method) {
    self::$methods[] = $method;
  }

  private function __call($method, $args) {
    if (in_array($method, self::$methods)) {
      return call_user_func_array($method, $args);
    }
  }
}

function test() {
  print "Hello World" . PHP_EOL;
}

Dynamic::registerMethod('test');
$d = new Dynamic();
$d->test();
?>

and i dont understand here
line 6 (self:: ), 10, 11 and the purpose of __call function
thanks beforehands

The static method is:

public static function registerMethod($method) {
    self::$methods[] = $method;
  }

What it does is take a string $method as parameter, and append it to the array self::$methods . self::$methods refers the static property defined in:

class Dynamic {
  static protected $methods = array();

The method __call defined in:

class Dynamic {
  private function __call($method, $args) {
    if (in_array($method, self::$methods)) {
      return call_user_func_array($method, $args);
    }
  }

Is a "magic" method. It is a method that is called when ever a dynamic method is called on that class but has not been defined.

It is similar to the "__get" and "__set" methods which are used for dynamic properties.
http://php.net/manual/en/language.oop5.magic.php

So if you call a method that doesn't exist, example:

$d = new Dynamic();
$d->test();

Normally you would get a fatal error. But with PHP5, the class will first look for the method __call() and if it finds it, it will call that method to see if it will handle the method call in a custom way.

In this case the custom way is:

private function __call($method, $args) {
    if (in_array($method, self::$methods)) {
      return call_user_func_array($method, $args);
    }
  }

Which is to look in the self::$methods static property for the name of the method. If it is defined, then call the function by using call_user_func_array().

So the example you have will call the function test().

thank you very much
but here one think i didnt understand
from where did this fucntion came
so we didnt defined it anywhere

call_user_func_array($method, $args)

Thank you for attention

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.