0

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?

6
  • Could you give a real example? Commented Jan 11, 2011 at 12:55
  • Can you point to resource you have seen it? The question is a bit out of context. Commented Jan 11, 2011 at 12:55
  • It usually is used for _examples_$ Commented Jan 11, 2011 at 12:58
  • May be the implementaion of body of these functions are intended to be defined in the extended (inherited) class Commented Jan 11, 2011 at 12:58
  • Maybe it's functions within a class that's inteded to get overwritten from another class extending it you've seen...? Never seen alot of empty functions being used Commented Jan 11, 2011 at 13:02

4 Answers 4

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

2 Comments

Space is not really limited, you would just create a separate release.
and what if you add several methods you plan to implement, release, then decide not to implement them, you have a release with lots of crap that will never be used, as i said you would just create a separate release
0

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'.

Comments

-1

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";
}
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.