• mlotfi63

    (@mlotfi63)


    why __construct() doesn’t work in a wordpress plugin?

    Check the codes below. This work correctly as a php code, but doesn’t work as/in wordpress plugin:

    <?php
    /*
    Plugin Name: myClass
    Plugin URI:
    Description: myClass
    Version:
    Author:
    Author URI:
    */
    class myClass {
    function __construct() {
    echo(‘this is __construct’);
    }
    }
    $myClass = new myClass();
    ?>

    Please Help, Thanks…

Viewing 7 replies - 1 through 7 (of 7 total)
  • add exit; below the echo and see what happens. Sometimes with WordPress you have to kill the script to see echoed output.

    Thread Starter mlotfi63

    (@mlotfi63)

    ECHO is not important, running __construct() function is importance, could you help me?

    ECHO is not important, running __construct() function is importance, could you help me?

    Not if you don’t pay attention.

    Of course the echo isn’t important! Seriously…

    You think the __construct() doesn’t work because you don’t see the echo right? But as I explained to you before, sometimes you have to kill the script to see the echo. I can’t remember the exact circumstances but I have ran into the need to kill the script to see the debug data. In other words, you might not be seeing the echo even if the __construct works. So do what I said and put the exit below the echo and see what happens. If script breaks then the construct works and your problem is elsewhere.

    That said, I just copied your code into my plugins directory and, well, it works. I see ‘this is __construct’ at the top of the page. You did activate the plugin, right? And you are running PHP5 and not PHP4, right?

    Thread Starter mlotfi63

    (@mlotfi63)

    Doesn’t work, even by killing the script.
    I’m using PHP5, (PHP 5.2.8) and WP 2.8.6
    Thanks….

    Do you mean that the script died and you still didn’t see the echo? Or that nothing happened? Or…?

    I don’t think it is the __construct. Like I said, your code works for me. I copied your code from above, pasted it into myclass.php in my wp-content/plugins folder, activated the plugin and it works. ( I’m using PHP 5.2.5 and WP 2.8.6, but it also works on 2.8.5. )

    There must be something else causing the problem. Can you get anything to work? Can you cause a syntax error, for example?

    Thread Starter mlotfi63

    (@mlotfi63)

    hmmmmmm, no, I’m using my all wordpress without any problem, all plugins, …, features are working correctly here https://www.paiz.ir (This is Persian [fa_IR]). Even I cleaned up my web server and reinstall wordpress, but…
    Off course the ___construct works on my localhost web server in same wordpress versions, but on the web not…

    Which means there is something different about the web hosting environment vs. the local server environment. Try to add this function to your class and see what happens.

    function myClass() {
    echo('this is __construct');
    }
Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘__construct()’ is closed to new replies.