Hướng dẫn __autoload in php w3schools

Hướng dẫn __autoload in php w3schools
Hướng dẫn __autoload in php w3schools

    • Share

That's happen is what then I go the second time at creating the class in the script I get an error from my execute method telling me "Cannot redeclare __autoload()" which I have in the method.My understanding with classes and functions, is what the content of the function/class, method don't have any relation with outstanding data. so how can __autoload be set then this is a new instant in the code?

Link to comment
Share on other sites

Hướng dẫn __autoload in php w3schools
Hướng dẫn __autoload in php w3schools

    • Share

I can't really make sence of your post...You can't declare the same class or function twice. Seems logical enough, doesn't it?If a class is not declared, PHP will run a function (one global function; not a class method) named "__autoload()" if there is one declared, and will retry to load the class again. It is expected of the __autoload() function to do whatever is necesary to load the class given to it as a first argument. Typically, this will mean including a certain file.

Link to comment
Share on other sites

Hướng dẫn __autoload in php w3schools
Hướng dẫn __autoload in php w3schools

  • Author

    • Share

I can't really make sence of your post...You can't declare the same class or function twice. Seems logical enough, doesn't it?If a class is not declared, PHP will run a function (one global function; not a class method) named "__autoload()" if there is one declared, and will retry to load the class again. It is expected of the __autoload() function to do whatever is necesary to load the class given to it as a first argument. Typically, this will mean including a certain file.

class classname{public function execute(){function __autoload(){somestuff}somestuff}}$class = new classname();$class->execute();//Working$class = new classname();$class->execute();//error autoload already defined

Link to comment
Share on other sites

Hướng dẫn __autoload in php w3schools
Hướng dẫn __autoload in php w3schools

    • Share

Move the __autoload() outside of the class.Read my explanation above again in regards to how __autoload works and/or see the PHP manual page about it.

Link to comment
Share on other sites

Hướng dẫn __autoload in php w3schools
Hướng dẫn __autoload in php w3schools

  • Author

    • Share

Move the __autoload() outside of the class.Read my explanation above again in regards to how __autoload works and/or see the PHP manual page about it.

Well I want it in my class cause of the different rules it has to have. I'm loading classes in the class. and not outside the class.

Link to comment
Share on other sites

Hướng dẫn __autoload in php w3schools
Hướng dẫn __autoload in php w3schools

    • Share

You can do that with appropriate include_once statements inside the class.

Link to comment
Share on other sites

Hướng dẫn __autoload in php w3schools
Hướng dẫn __autoload in php w3schools

  • Author

    • Share

You can do that with appropriate include_once statements inside the class.

I do not follow you, how would this help the __autoload function then I make a new instants of the class it collides with the already loaded __autoload function which only exists in the first instant.

Link to comment
Share on other sites

Hướng dẫn __autoload in php w3schools
Hướng dẫn __autoload in php w3schools

    • Share

Forget about __autoload for a second... If the classname::execute() methods needs to load a class, it can safely do so with by including a file that contains it. That file should of course contain ONLY the class to be loaded. To prevent the error message for redeclaration, use include_once instead of include. That way, the class will only be loaded if it's not already.If each method similar to classname::execute() needs to load a different class in a certain different fashion, you can implement that different logic within the file itself, leading up to the include_once OR a plain declaration surrounded by a condition for class_exists() to prevent redeclarations.The __autoload() function is about providing a uniform way of loading non-existant classes.

Link to comment
Share on other sites

Hướng dẫn __autoload in php w3schools
Hướng dẫn __autoload in php w3schools

  • Author

    • Share

Forget about __autoload for a second... If the classname::execute() methods needs to load a class, it can safely do so with by including a file that contains it. That file should of course contain ONLY the class to be loaded. To prevent the error message for redeclaration, use include_once instead of include. That way, the class will only be loaded if it's not already.If each method similar to classname::execute() needs to load a different class in a certain different fashion, you can implement that different logic within the file itself, leading up to the include_once OR a plain declaration surrounded by a condition for class_exists() to prevent redeclarations.The __autoload() function is about providing a uniform way of loading non-existant classes.

Well the thing is I don't know what files to include cause it's dynamic, but sure I could make a script what would function like the __autoload.Something like check if the class exists if not include the file just like how the __autoload would do and execute the class after the include.This will result in some more lines in the code...

Link to comment
Share on other sites

Hướng dẫn __autoload in php w3schools
Hướng dẫn __autoload in php w3schools

  • Author

    • Share

Well the thing is I don't know what files to include cause it's dynamic, but sure I could make a script what would function like the __autoload.Something like check if the class exists if not include the file just like how the __autoload would do and execute the class after the include.This will result in some more lines in the code...

Well same sort of error.This time I made a function generating the path way.. mostly cause not having to rewrite to much of the code and it seems any function in the class method will generate the error of already set function.

Link to comment
Share on other sites

Hướng dẫn __autoload in php w3schools
Hướng dẫn __autoload in php w3schools

  • Author

    • Share

Well same sort of error.This time I made a function generating the path way.. mostly cause not having to rewrite to much of the code and it seems any function in the class method will generate the error of already set function.

Found this on an other web page as an answer to a similar question:

It can be done, but since functions are defined in the global scope this will result in an error if the method is called twice since the PHP engine will consider the function to be redefined during the second call.

Edited February 6, 2011 by ckrudelux

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Sign in

Already have an account? Sign in here.

Sign In Now