1
<?php

$myClass = new MyClass;
$myClass->myFunc();

class MyClass 
{
    public static function myFunc() {
        echo 'testcall';
    }
}

?>

Does php go from top to bottom?

If so why does php know MyClass at the moment where I create an instance of it?

1
  • 1
    php goes from top to bottom but when if found any function,variable declaration or class instantiate then it will load those first and then go to next line. Commented Aug 11, 2016 at 10:02

2 Answers 2

1

There is no need of forward declaration in PHP, instead you need to have the class declared in current script even if it's after the object invocation. but for any included script include statement needs to be executed before you create instance of that class.

That's why your code works.

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

Comments

1

In PHP there is no need of declaration previously, You can use it at the time of using it that variable.Your code is working as you included it in the same file

At the time of declaring class in another .php file you have to include it compulsory before initiating the object of that particular class.

Hope it is useful to you :)

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.