Forums

__construct() (8 posts)

  1. mlotfi63
    Member
    Posted 2 years ago #

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

  2. apljdi
    Member
    Posted 2 years ago #

    add exit; below the echo and see what happens. Sometimes with WordPress you have to kill the script to see echoed output.

  3. mlotfi63
    Member
    Posted 2 years ago #

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

  4. apljdi
    Member
    Posted 2 years ago #

    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?

  5. mlotfi63
    Member
    Posted 2 years ago #

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

  6. apljdi
    Member
    Posted 2 years ago #

    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?

  7. mlotfi63
    Member
    Posted 2 years ago #

    hmmmmmm, no, I'm using my all wordpress without any problem, all plugins, ..., features are working correctly here http://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...

  8. apljdi
    Member
    Posted 2 years ago #

    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');
    }

Topic Closed

This topic has been closed to new replies.

About this Topic