Create an array inside a class with php
I have a class and I need to build an array. I usually can do that with a query and looping that but I am not sure how to do that with a class. Do I create the array before hand and turn it into a string? Here is my code sample
/** My Setting class **/ if ( ! class_exists( 'WC_my_setting' ) ) < class WC_my_setting < public function __construct() < // Init settings $this->settings = array( array( 'name' => __( 'My Setting', 'woocommerce-my-setting' ), 'type' => 'title', 'id' => 'wc_my_setting_options' ), array( 'name' => __( 'Name', 'woocommerce-my-setting' ), 'id' => 'wc_my_setting_category', 'type' => 'multiselect', //I want to have this->settings[options] populated with the array that the $category_options makes. 'options' => $category_options ), ) ), array( 'type' => 'sectionend', 'id' => 'woocommerce-my-setting' ), ); // Default options add_option( 'wc_my_setting_category', '' ); // Admin add_action( 'woocommerce_settings_image_options_after', array( $this, 'admin_settings' ), 20 ); add_action( 'woocommerce_update_options_catalog', array( $this, 'save_admin_settings' ) ); add_action( 'woocommerce_update_options_products', array( $this, 'save_admin_settings' ) ); >
I can the code below on page templates just fine and I can output the array fine but I cannot get this working in the class in the admin area:
$categories = get_terms( 'product_cat', 'orderby=name&hide_empty=0' ); $category_options = array(); if ( $categories ) < foreach ( $categories as $cat ) < $category_options[ $cat->slug ] = $cat->name; > > // print_r($category_options);
How do I get the above loop in my class so the $category_options is placed in the $this->settings[options]
How to pass array to a class in php?
So when i call this function ispis() it doesn’t do anything but when i echo inside function __constructor it shows correct values of everything entered. Also when i comment first three lines above //$daniel = new User(«ivan»,»22″); line and uncomment this, ispis() works just fine. Would be nice if someone could explain to me why this is happening. Tnx in advance 🙂
$question_array is an array of User objects. So what exactly are you expecting $daniel = new User($question_array); to do?
You have defined 4 parameters on your constructor while you are passing only one to the last user instance . 4 parameters are expected ?
Also, there is no way that $daniel = new User(«ivan»,»22″); «works just fine». Depending on your version of PHP, you are, at best, getting a Warning, if not a Fatal Error.
3 Answers 3
By the looks of your code you’re trying to pass two new User instances into a new user («daniel»).
So basically User is expecting 4 arguments (age, name, height, weight). You’ve created Luka and Ivan correctly, but you’re passing those two Users as arguments when trying to create Daniel. You’re giving it Luka and Ivan when it wants age, name, height and weight.
If you simply want to pass an array to the constructor, just pass it as an argument on the new instance:
age = $args['age']; $this->name = $args['name']; $this->height = $args['height']; $this->weight = $args['weight']; > function getAge() < return $this->age; > > $question_array = [ 'name' => 'Daniel', 'age' => '22', 'weight' => '174', 'height' => '68' ]; $daniel = new User($question_array); echo $daniel->getAge(); // 22 ?>
Using Array in PHP Class
I am exercising some PHP OOP and therefore I am creating a class to create a simple navigation menu ( with extensions in the future ) now I have build this class that works kinda.. with 1 menu item tough.. I don;t know how to use arrays in my class to use the class like
setMenuItem("home", "test"); echo $menu->display(); ?>
as you see I should be able to give each menu item with the setMenuItem(); method. but since it does not use Arrays at the moment I only get the first value The class itself is as follows:
who can show me how to use arrays within the class in combination with a loop to create a menu with all given values? Thanks in advance.
4 Answers 4
You actually have two types, not one:
The MenuItemList would take care of managing the list. It could use an array internally. A code example for something very similar could be found in a previous answer: Array Object In Php.
Next to that the display() method does not belong into the two. Instead you should make your template that keen it knows how to output a menu list:
echo '
'; foreach ($menu as $item) < echo ')" data-controller="se-share-sheet" data-se-share-sheet-title="Share a link to this answer" data-se-share-sheet-subtitle="" data-se-share-sheet-post-type="answer" data-se-share-sheet-social="facebook twitter devto" data-se-share-sheet-location="2" data-se-share-sheet-license-url="https%3a%2f%2fcreativecommons.org%2flicenses%2fby-sa%2f3.0%2f" data-se-share-sheet-license-name="CC BY-SA 3.0" data-s-popover-placement="bottom-start">Share)">edited May 23, 2017 at 10:26 CommunityBot11 silver badgeanswered Nov 4, 2012 at 17:31 hakrehakre)" >192k52 gold badges434 silver badges834 bronze badges