Fiasco
Fiasco

Reputation: 33

how can blank functions help us in PHP

hello
I'm learning php and i have seen many empty Function in many other small projects and I don't know how it can help.

Example:

public function first(){
// do nothing
          }
public function second(){
        // do nothing
          }
public function myfuntion(){
        // Codes
          }

another example:
class user_bo{

protected $attributes = Array(); 

  public function __get($key){  

      return array_key_exists($key, $this->attributes) ? $this->attributes[$key] : null;   
  } 

  public function __set($key, $value){  

      $this->attributes[$key] = $value;   
  } 

  /** Constructor **/
  public function __construct(){ }  

} ?>

how can __construct() help us in this situation since it look like it doesn't do anything?

Upvotes: 0

Views: 143

Answers (4)

RobertPitt
RobertPitt

Reputation: 57268

When you say Empty functions, more than lily your talking about __construct and magic methods

The way they help us to determine authorization of an action, for example

public class MyClass
{
    private function __construct(){}
}

This allows us to block the new MyClass call, but can be overriden by calling a static method and doing new self()

There are also other reasons such as disallowed inheritance, like so:

#Class 1
public function Base
{
    public function start()
    {
        echo "hello";
    }
}

#Class 2
public function Layer extends Base
{
}

#Class 3
public function Layer extends Base
{
    private function start(){}
}

looking at the above class 2 contains a callable method called start, but its the method from the parent, in class 3 the same occurs but its overridden by the local method as such, and as its private it disallows the call of the method.

getting a little deeper into OOP we have interfaces which tell a class it must contain a designated set of methods, so if a class implements such interface it will be required to define all the methods needed, otherwise it will throw an error, so programmers may create empty methods to keep the class structure.


In light of your new example, There is no specific reason for an empty public __constructor apart from when its extending another class.

Here's an example:

class Main
{
    public function __construct()
    {
        echo "Main";
    }
}

class FirstSub extends Main
{
}

class SecondSub extends Main
{
    public function __construct()
    {
        echo "SecondSub";
    }
}

class ThirdSub extends Main
{
    public function __construct()
    {
        echo "ThirdSub <br />";
        parent::__construct();
    }
}

As you can see from the structure we have one primary class called Main and three sub class.

If we initialize the main class were going to get the constructor print Main, but the results will vary with the sub classes as the constructor overrides the parent.

so initializeing the following will result in:

  • Main: Main
  • FirstSub: `` - Nothing
  • SecondSub:SecondSub
  • ThirdSub: ThirdSub<br />Main

as you can so that using a blank construct has effect on parent classes.

Upvotes: 2

hahaha
hahaha

Reputation: 476

Ahn, I guess you're reading about "functions in PHP". And here, author explain how to define your own and custom functions.

<?php
function foobar() {
    echo "Foobar";
}
?>

Upvotes: -1

Or Weinberger
Or Weinberger

Reputation: 7482

I'm not sure if I got what you tried to explain, but sometimes it helps a lot of people to code the general outlines of a program, and only after they're done to complete and code each function so it won't cut off their line of thinking.

To remember that they need to code that function, they create it but leave it empty. Of course the names of the functions will probably be more descriptive than 'first', 'second' or 'myfunction'.

Upvotes: 0

Yada
Yada

Reputation: 31225

The blank functions are probably not implemented yet and are probably used as place holders for future enhancement.

Upvotes: 0

Related Questions