1

I have the following code, perform a global function within a class to fill the functions of wordpress, the problem is that the only way that I could get a variable public class is as follows

class Core {
 public $notice;
 function __construct(){
  $this->core_function();

 }
 function core_function(){
   global $globalvar;
   $globalvar = $this;
   function notice_global(){
    global $globalvar;

    return $globalvar->notice;

   } 
 }
 function set_notice(){
  $this->notice = array('Warning');
 }

}

$GP = new Core();
$GP->set_notice();
var_dump(notice_global());

Any other ideas or suggestions, this code is correct or not?

6
  • Have you tried it before asking? var_dupm() doesn't exists. Commented Feb 18, 2016 at 18:47
  • 1
    @fusion3k that's just a typo. @OP why not $GP->notice? It's a public variable. Commented Feb 18, 2016 at 18:57
  • As @Flyer says, you can call directly $GP->notice and delete __construnc, core_function and notice_global. There is a particular reason to call notice_global() instead of $GP->notice Commented Feb 18, 2016 at 19:11
  • @fusion3k was my mistake is var_dump , Yes, I moved notice_global out class because I need to fill this function with WordPress own functions and do not want to create a new call to the class whenever you need to occupy Commented Feb 18, 2016 at 19:48
  • You cannot restrict the scope of methods by declaring them inside each other. So, It is confusing you and others by writing notice_global() inside function core_function(). PHP should raise a strict error. PHP will treat it as a normal method! So what you have coded is wrong in PHP. However, because you have used the global $globalvar;, which is such a wrong thing to be doing, the code will work correctly! ;-/ Commented Feb 19, 2016 at 1:02

3 Answers 3

2

As you said in the comments, you need global function due to wordpress hook method (for a plugin, I suppose).

This is not necessary: there is a way to pass an object method (not a whole object) to wordpress.

You can try in this way:

class Core {
    public $notice;

    function get_notice()
    { return $this->notice; }

    function set_notice()
    { $this->notice = array('Warning'); }
}

$GP = new Core();
$GP->set_notice();

add_action( 'save_post', array( $GP, 'get_notice' ) );

Or - for a better flexibility - in this way:

class Core {
    public $notice;

    function get_notice()
    { return $this->notice; }

    function set_notice()
    { $this->notice = array('Warning'); }

    function add_wp_action( $hook, $method )
    { add_action( $hook, array( $this, $method ) ); }
}

$GP = new Core();
$GP->set_notice();

$GP->add_wp_action( 'save_post', 'get_notice' );

By this way, you can directly set all your wp hooks in the class and call they directly with an object method, without using globals variables or function tricks.

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

Comments

0

I'm not sure if I'm understanding you right, but notice_global can be moved out of that class.

Globals have scope outside of classes

1 Comment

clear the idea is to move out for global use, since I want to take with WordPress functions without creating a new instance, the main question is whether this can cause an overload or improperly'm taking this
0

There is no need for these functions. You've defined $notice as public property. You can access it like this: $GP->notice;

You might also want to read documentation on visibility of methods and properties.

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.