treehouse : what would you like to learn today?
Web Design Web Development iOS Development

Problem with Classes

  • I'm having a problem with two classes working together

    These are my classes


    class calssOne {

    public function functionOne(){
    $myArray = array(
    array( 'std' => '' , 'type' => 'checkbox' , 'pre' => 'prefix_' , 'name' => 'theName' )
    );

    foreach($myArray as $value){
    $foreachArray = array( $value['pre'],$value['name'] => $value['std']);
    }
    }
    }

    class classTwo {
    public function functionTow() {

    $get_class = new calssOne();
    $myOptions = $get_class -> functionNameOne();

    if (isset($_POST['update_options'])) {
    if (isset($_POST['theName'])) { // -------- foreach loop --------
    $myOptions['prefix_theName'] = apply_filters('prefix_theName_save_pre', $_POST['theName']);
    }
    }
    }
    }


    That code works, but I want to make a foreach loop for the if statement with the comment.

    Everything I try doesn't work.

    Does anyone have any ideas for this?
  • That code shouldn't work....or at least it is written incorrectly

    $myOptions never actually equals anything because functionNameOne doesn't return a value.

    So
    $myOptions = $get_class -> functionNameOne();

    should probably be
    $get_class -> functionNameOne();
    or you should return a value from the functionNameOne, which I'm guessing would be the $foreachArray.

    What do you want the foreach loop to do anyway. And is this for Wordpress? Because apply_filters() isn't a standard PHP function

  • Yes it is for a WordPress plugin, but I'm just trying to learn OOP. It's based off a this tutorial.

    The exapmle I'm working off of is here. The person who wrote the tutrial doesn't explain why he used that method, but it does work.
  • Basically I want the foreach to do is loop this.

    if (isset($_POST['theName'])) {
    $myOptions['prefix_theName'] = apply_filters('prefix_theName_save_pre', $_POST['theName']);
    }


    for each item in $myArray.


    I removed the $myOptions var and it doesn't.