Defining a class with a method

XGHeaven

If I define a class LOG like this:

class LOG {
    static public function LOG(){
        return;
    }
}
echo 1;

When I run it, it shows nothing. If I comment out the method like this:

class LOG {
    /*static public function LOG(){
        return;
    }*/
}
echo 1;

It will show "1" in browser. Why doesn't the first code snippet work?

Darren

Edit

When you turn error reporting on, you'll get this error:

Fatal error: Constructor LOG::LOG() cannot be static

Removing the static keyword in front of the function will work:

class LOG{
    public function LOG(){
        return;
    }
}

Example


In PHP, when you create a class like this:

class LOG{
    static public function LOG(){
        return;
    }
}

(With the same named function as the class)

It will run that function as the __construct() method. It doesn't reach the echo because of the return; statement you have in there.

If you comment it out, it should work:

class LOG{
    static public function LOG(){
        // return;
        echo 'inside class';
    }
}

But I don't think that's the error, there has to be something else. Turn on error reporting and let us know if there are any errors.

<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

defining method of class in if statement

Conditionally defining method for container class

Defining a javascript class Method as a constant

Error for defining class method as expression

defining part of a method in a parent class without defining the return

Defining custom gradient as a class method in Tensorflow

Defining all abstract method in one concrete class

Java: Defining a generic method inside an anonymous class

Defining an abstract method in a SQLAlchemy base class

Swift protocol defining class method returning self

Determine the defining class from within a method

How to get the defining class of a bound method?

python method for defining indexing in a class (use of "[ ]")

Python: re-defining a class method at runtime

Python a class method and defining a default value (if param is empty) and static method

Is there any solution to handle Transactional by defining in Abstract Class of Concrete Method?

Defining python class method using arguments from __init__

Scala what is the difference between defining a method in the class instead on the companion object

Get defining class of unbound method object in Python 3

Defining a Class method vs Passing object as parameter to an Function in OOP

Serialize objects with inheritance by defining serialize method only in base class?

How to get a method’s defining class’s name?

CDI producer method not working if defining class doesn't declare a scope

When defining class attributes, which method of writing is more "Pythonic"?

javascript : Assign a method to another object without defining that method in second class body

Defining and calling a method in irb

I want to implement generic java method passing argument as class object and defining method parameter as Class<T> or T type

Defining "boolness" of a class in python

Defining a class with or without the .prototype