├── PHP_INSTALLATION.md ├── README.md ├── create_currency_table.php ├── create_currency_table_db.php ├── create_iso_country_codes_csv.php ├── db.config.php ├── google_api_key.php ├── info.php ├── php7cookbook.sql ├── phpunit.phar ├── prospects.csv ├── source ├── .buildpath ├── .project ├── .settings │ └── org.eclipse.php.core.prefs ├── Application │ ├── Acl │ │ ├── Acl.php │ │ ├── Authenticate.php │ │ ├── AuthenticateInterface.php │ │ └── DbTable.php │ ├── Autoload │ │ └── Loader.php │ ├── Cache │ │ ├── CacheAdapterInterface.php │ │ ├── Constants.php │ │ ├── Core.php │ │ ├── Database.php │ │ └── File.php │ ├── Captcha │ │ ├── CaptchaInterface.php │ │ ├── Image.php │ │ ├── Phrase.php │ │ └── Reverse.php │ ├── Database │ │ ├── BasicOps.php │ │ ├── Connection.php │ │ ├── ConnectionAwareInterface.php │ │ ├── ConnectionTrait.php │ │ ├── CustomerOrmService_1.php │ │ ├── CustomerOrmService_2.php │ │ ├── CustomerService.php │ │ ├── Finder.php │ │ ├── Mapper │ │ │ ├── FieldConfig.php │ │ │ └── Mapping.php │ │ ├── Paginate.php │ │ ├── Search │ │ │ ├── Criteria.php │ │ │ └── Engine.php │ │ └── Service.php │ ├── Entity │ │ ├── Address.php │ │ ├── Base.php │ │ ├── Customer.php │ │ ├── Name.php │ │ ├── Person.php │ │ ├── Product.php │ │ ├── Profile.php │ │ ├── ProtectedPerson.php │ │ ├── PublicPerson.php │ │ └── Purchase.php │ ├── Error │ │ ├── GeneratesError.php │ │ ├── Handler.php │ │ ├── ThrowsError.php │ │ └── ThrowsException.php │ ├── Event │ │ ├── Listener.php │ │ └── Manager.php │ ├── Filter │ │ ├── AbstractFilter.php │ │ ├── CallbackInterface.php │ │ ├── Filter.php │ │ ├── Messages.php │ │ ├── Result.php │ │ └── Validator.php │ ├── Form │ │ ├── Element │ │ │ ├── CheckBox.php │ │ │ ├── Form.php │ │ │ ├── Radio.php │ │ │ └── Select.php │ │ ├── Factory.php │ │ └── Generic.php │ ├── Generic │ │ ├── CountryList.php │ │ ├── CountryListUsingTrait.php │ │ ├── CustomerList.php │ │ ├── CustomerListUsingTrait.php │ │ ├── Hydrator │ │ │ ├── Any.php │ │ │ ├── GetSet.php │ │ │ └── Strategy │ │ │ │ ├── Extending.php │ │ │ │ ├── GetSet.php │ │ │ │ ├── HydratorInterface.php │ │ │ │ └── PublicProps.php │ │ ├── ListFactory.php │ │ ├── ListTrait.php │ │ ├── Name.php │ │ ├── Node.php │ │ ├── Registry.php │ │ ├── Search.php │ │ ├── Stack.php │ │ ├── Temp.php │ │ ├── Test.php │ │ └── Tree.php │ ├── I18n │ │ ├── Calendar.php │ │ ├── Day.php │ │ ├── Event.php │ │ ├── IsoCodes.php │ │ ├── IsoCodesCsv.php │ │ ├── IsoCodesDb.php │ │ ├── IsoCodesInterface.php │ │ ├── Locale.php │ │ ├── SingleEvent.php │ │ └── Translate │ │ │ ├── Adapter │ │ │ ├── Csv.php │ │ │ ├── Database.php │ │ │ ├── Ini.php │ │ │ ├── TranslateAdapterInterface.php │ │ │ └── TranslateAdapterTrait.php │ │ │ └── Translation.php │ ├── Iterator │ │ ├── Directory.php │ │ └── LargeFile.php │ ├── MiddleWare │ │ ├── Constants.php │ │ ├── Message.php │ │ ├── Request.php │ │ ├── Response.php │ │ ├── ServerRequest.php │ │ ├── Session │ │ │ └── Validator.php │ │ ├── Stream.php │ │ ├── TextStream.php │ │ ├── UploadedFile.php │ │ └── Uri.php │ ├── Mvc │ │ ├── City.php │ │ └── Dispatch.php │ ├── Parse │ │ ├── Convert.php │ │ ├── ConvertXml.php │ │ ├── Tree.php │ │ ├── WebTree.php │ │ └── XmlPath.php │ ├── PubSub │ │ ├── Publisher.php │ │ └── Subscriber.php │ ├── Routing │ │ └── Router.php │ ├── Security │ │ ├── PassGen.php │ │ └── SessOptions.php │ ├── Test │ │ ├── FakeData.php │ │ └── TestClass.php │ └── Web │ │ ├── AbstractHttp.php │ │ ├── Access.php │ │ ├── Client │ │ ├── Curl.php │ │ └── Streams.php │ │ ├── Deep.php │ │ ├── Hoover.php │ │ ├── Menu │ │ ├── Factory.php │ │ ├── Item.php │ │ └── Menu.php │ │ ├── Received.php │ │ ├── Request.php │ │ ├── Response.php │ │ ├── Rest │ │ ├── AbstractApi.php │ │ ├── ApiInterface.php │ │ ├── CustomerApi.php │ │ └── Server.php │ │ ├── Security.php │ │ └── Soap │ │ └── ProspectsApi.php ├── Psr │ ├── Http │ │ └── Message │ │ │ ├── MessageInterface.php │ │ │ ├── RequestInterface.php │ │ │ ├── ResponseInterface.php │ │ │ ├── ServerRequestInterface.php │ │ │ ├── StreamInterface.php │ │ │ ├── UploadedFileInterface.php │ │ │ └── UriInterface.php │ ├── LICENSE │ ├── README.md │ └── composer.json ├── chapter01 │ ├── chap_01_autoload_test.php │ ├── chap_01_deep_scan_website.php │ ├── chap_01_php5_to_php7_code_converter.php │ ├── chap_01_php5_to_php7_code_converter_test_file.php │ ├── chap_01_php7_to_php5_code_converter.php │ ├── chap_01_php7_to_php5_code_converter_test_file.php │ ├── chap_01_vacuuming_website.php │ └── test.php ├── chapter02 │ ├── chap_02_config.php │ ├── chap_02_foreach.php │ ├── chap_02_iterating_through_a_massive_file.php │ ├── chap_02_performance_using_php7_enhancement_call.php │ ├── chap_02_recursive_directory_iterator.php │ ├── chap_02_simple_ast_example.php │ ├── chap_02_taking_advantage_of_ast.php │ ├── chap_02_taking_advantage_of_ast_left_to_right_parsing.php │ ├── chap_02_understanding_ast_simple_example.php │ ├── chap_02_understanding_diffs_in_parsing.php │ ├── chap_02_understanding_diffs_in_parsing_2.php │ ├── chap_02_understanding_diffs_in_parsing_php5.php │ ├── chap_02_understanding_variable_variables.php │ ├── chap_02_uploading_csv_to_database.php │ ├── chap_02_web_filtering_ast_example.php │ ├── css │ │ ├── bootstrap.css │ │ ├── owl.carousel.css │ │ ├── popuo-box.css │ │ └── style.css │ ├── home.php │ ├── images │ │ ├── arrow-2.png │ │ ├── arrow1.png │ │ ├── brand-logo1.png │ │ ├── brand-logo2.png │ │ ├── brand-logo3.png │ │ ├── brand-logo4.png │ │ ├── btn-icons.png │ │ ├── divice-half.png │ │ ├── divice-in-hand.png │ │ ├── divice.png │ │ ├── fav.png │ │ ├── feature-icons-l.png │ │ ├── feature-icons-r.png │ │ ├── heart-icon.png │ │ ├── icon1.png │ │ ├── logo.png │ │ ├── msg-icon.png │ │ ├── nav-icon.png │ │ ├── play-btn.png │ │ ├── quit-people.jpg │ │ ├── screen-coursuals.png │ │ ├── screen.png │ │ ├── slide.jpg │ │ ├── social-icons.png │ │ ├── team-member.jpg │ │ ├── team-member1.jpg │ │ ├── team-member2.jpg │ │ ├── team-member3.jpg │ │ ├── team-member4.jpg │ │ ├── team-social.png │ │ ├── testmonials.png │ │ ├── to-top1.png │ │ └── top-slider-pagenate.png │ ├── index.html │ ├── js │ │ ├── easing.js │ │ ├── jquery.magnific-popup.js │ │ ├── jquery.min.js │ │ ├── move-top.js │ │ ├── owl.carousel.js │ │ └── responsiveslides.min.js │ ├── templates │ │ └── home.html │ └── w3layouts-license.txt ├── chapter03 │ ├── chap_03_developing_functions.php │ ├── chap_03_developing_functions_array_iterator.php │ ├── chap_03_developing_functions_filtered_and_paginated.php │ ├── chap_03_developing_functions_filtered_iterator.php │ ├── chap_03_developing_functions_iterators.php │ ├── chap_03_developing_functions_iterators_library.php │ ├── chap_03_developing_functions_library.php │ ├── chap_03_developing_functions_namespace.php │ ├── chap_03_developing_functions_namespace_alpha.php │ ├── chap_03_developing_functions_namespace_beta.php │ ├── chap_03_developing_functions_paginated.php │ ├── chap_03_developing_functions_recursive.php │ ├── chap_03_developing_functions_return_types.php │ ├── chap_03_developing_functions_return_types_library.php │ ├── chap_03_developing_functions_stacked_iterators.php │ ├── chap_03_developing_functions_type_hints_library.php │ ├── chap_03_developing_functions_using_generator.php │ ├── chap_03_developing_functions_with_hints.php │ └── test.php ├── chapter04 │ ├── NameAddress.php │ ├── Test.php │ ├── TwoClass.php │ ├── chap_04_oop_abstract.php │ ├── chap_04_oop_anonymous_class.php │ ├── chap_04_oop_anonymous_class_basic.php │ ├── chap_04_oop_anonymous_class_extends.php │ ├── chap_04_oop_anonymous_class_interfaces.php │ ├── chap_04_oop_basic_visibility.php │ ├── chap_04_oop_classes_case_insensitive.php │ ├── chap_04_oop_defining_class_test.php │ ├── chap_04_oop_extends.php │ ├── chap_04_oop_factory_pattern_example.php │ ├── chap_04_oop_interfaces_collisions.php │ ├── chap_04_oop_late_static_binding_illustration.php │ ├── chap_04_oop_namespace_and_autoloading.php │ ├── chap_04_oop_namespace_example.php │ ├── chap_04_oop_namespace_example_1.php │ ├── chap_04_oop_namespace_example_2.php │ ├── chap_04_oop_namespace_example_3.php │ ├── chap_04_oop_private.php │ ├── chap_04_oop_simple_interfaces_example.php │ ├── chap_04_oop_simple_static.php │ ├── chap_04_oop_single_inheritance.php │ ├── chap_04_oop_trait_anonymous_class.php │ ├── chap_04_oop_trait_methods_do_not_override_class_methods.php │ ├── chap_04_oop_trait_multiple.php │ ├── chap_04_oop_traits_override_inherited.php │ ├── chap_04_oop_two_classes_in_one_file.php │ ├── chap_04_oop_type_hint.php │ ├── chap_04_oop_using_stdclass.php │ └── chap_04_oop_visibility_class_constants.php ├── chapter05 │ ├── chap_05_entity_to_query_fetch_by_id.php │ ├── chap_05_entity_to_query_remove.php │ ├── chap_05_entity_to_query_save.php │ ├── chap_05_jquery_datatables_php_lookups.php │ ├── chap_05_jquery_datatables_php_lookups_ajax.php │ ├── chap_05_match_entity_to_table.php │ ├── chap_05_oop_query_builder.php │ ├── chap_05_pagination.php │ ├── chap_05_pdo_connect_mysql.php │ ├── chap_05_pdo_connect_mysql_extended_dsn.php │ ├── chap_05_pdo_connect_pgsql.php │ ├── chap_05_pdo_connect_sqlite.php │ ├── chap_05_pdo_error_mode.php │ ├── chap_05_pdo_mysql_fetch_class.php │ ├── chap_05_pdo_mysql_fetch_class_fetch_object.php │ ├── chap_05_pdo_mysql_fetch_into.php │ ├── chap_05_pdo_mysql_fetch_obj.php │ ├── chap_05_pdo_prepared_bind_params.php │ ├── chap_05_pdo_prepared_named_placeholders.php │ ├── chap_05_pdo_prepared_positional_placeholders.php │ ├── chap_05_pdo_scrollable_cursor.php │ ├── chap_05_pdo_transactions.php │ ├── chap_05_secondary_lookups.php │ └── test.php ├── chapter06 │ ├── chap_06_form_element_checkbox.php │ ├── chap_06_form_element_generator.php │ ├── chap_06_form_element_radio.php │ ├── chap_06_form_element_select.php │ ├── chap_06_form_factory.php │ ├── chap_06_menu_builder.php │ ├── chap_06_menu_builder_1.php │ ├── chap_06_menu_builder_2.php │ ├── chap_06_menu_builder_3.php │ ├── chap_06_post_data_config_callbacks.php │ ├── chap_06_post_data_config_messages.php │ ├── chap_06_post_data_filtering.php │ ├── chap_06_post_data_validation.php │ ├── chap_06_tying_filters_to_form.php │ ├── chap_06_tying_filters_to_form_definitions.php │ ├── php7cookbook.css │ ├── php7cookbook_html_table.css │ └── test.php ├── chapter07 │ ├── api_key.php │ ├── chap_07_converting_php_array_to_xml.php │ ├── chap_07_converting_xml_php_iteration_1.php │ ├── chap_07_converting_xml_php_iteration_2.php │ ├── chap_07_converting_xml_php_to_array.php │ ├── chap_07_converting_xml_php_xpath.php │ ├── chap_07_payment_provider_request.php │ ├── chap_07_simple_rest_client_google_maps_curl.php │ ├── chap_07_simple_rest_client_google_maps_streams.php │ ├── chap_07_simple_rest_client_google_maps_template.php │ ├── chap_07_simple_rest_server.php │ ├── chap_07_simple_rest_server_test.php │ ├── chap_07_simple_soap_client_test.php │ ├── chap_07_simple_soap_client_weather_service.php │ ├── chap_07_simple_soap_server.php │ ├── chap_07_wsdl.xml │ ├── composer.json │ ├── composer.lock │ ├── composer.phar │ ├── google_api_key.php │ ├── php7cookbook.css │ ├── php7cookbook_html_table.css │ └── test.php ├── chapter08 │ ├── chap_08_control_and_combining_unicode.php │ ├── chap_08_emoji_using_html.php │ ├── chap_08_formatting_currency.php │ ├── chap_08_formatting_date.php │ ├── chap_08_formatting_numbers.php │ ├── chap_08_getting_locale_from_browser.php │ ├── chap_08_html_calendar.php │ ├── chap_08_html_calendar_full_year.php │ ├── chap_08_phpinfo.php │ ├── chap_08_recurring_events.php │ ├── chap_08_translation_array.php │ ├── chap_08_translation_csv.php │ ├── chap_08_translation_database.php │ ├── chap_08_translation_ini.php │ ├── jcartier-city.png │ ├── php7cookbook_html_table.css │ └── test.php ├── chapter09 │ ├── .gitignore │ ├── Application_MiddleWare_ServerRequest.php │ ├── chap_09_middleware_acl_config.php │ ├── chap_09_middleware_authenticate.php │ ├── chap_09_middleware_cache_db.php │ ├── chap_09_middleware_request.php │ ├── chap_09_middleware_request_to_response.php │ ├── chap_09_middleware_request_using_with.php │ ├── chap_09_middleware_server.php │ ├── chap_09_middleware_server_with_response.php │ ├── chap_09_middleware_value_objects_file_upload.php │ ├── chap_09_middleware_value_objects_uri.php │ ├── churchill.txt │ ├── composer.phar │ ├── customer_09_passwords.txt │ ├── customer_passwords.txt │ ├── expressive │ │ ├── .gitignore │ │ ├── .travis.yml │ │ ├── CHANGELOG.md │ │ ├── CONDUCT.md │ │ ├── CONTRIBUTING.md │ │ ├── LICENSE.md │ │ ├── README.md │ │ ├── composer.json │ │ ├── composer.lock │ │ ├── config │ │ │ ├── autoload │ │ │ │ ├── .gitignore │ │ │ │ ├── dependencies.global.php │ │ │ │ ├── local.php.dist │ │ │ │ ├── middleware-pipeline.global.php │ │ │ │ ├── routes.global.php │ │ │ │ ├── templates.global.php │ │ │ │ └── zend-expressive.global.php │ │ │ ├── config.php │ │ │ └── container.php │ │ ├── data │ │ │ └── .gitignore │ │ ├── phpcs.xml │ │ ├── phpunit.xml.dist │ │ ├── public │ │ │ ├── .htaccess │ │ │ ├── favicon.ico │ │ │ ├── index.php │ │ │ └── zf-logo.png │ │ ├── src │ │ │ └── App │ │ │ │ └── Action │ │ │ │ ├── HomePageAction.php │ │ │ │ ├── HomePageFactory.php │ │ │ │ ├── PingAction.php │ │ │ │ └── SessionValidateAction.php │ │ ├── templates │ │ │ ├── .gitkeep │ │ │ ├── app │ │ │ │ └── home-page.phtml │ │ │ ├── error │ │ │ │ ├── 404.phtml │ │ │ │ └── error.phtml │ │ │ └── layout │ │ │ │ └── default.phtml │ │ └── test │ │ │ └── AppTest │ │ │ └── Action │ │ │ ├── HomePageActionTest.php │ │ │ ├── HomePageFactoryTest.php │ │ │ └── PingActionTest.php │ ├── gettysburg.txt │ ├── info.php │ ├── pages │ │ ├── auth.php │ │ ├── logout.php │ │ ├── menu.php │ │ ├── page0.php │ │ ├── page1.php │ │ ├── page2.php │ │ ├── page3.php │ │ ├── page4.php │ │ ├── page5.php │ │ ├── page6.php │ │ ├── page7.php │ │ ├── page8.php │ │ ├── page9.php │ │ ├── route_menu.php │ │ └── sorry.php │ ├── public │ │ ├── index.php │ │ └── info.php │ ├── routing │ │ ├── .htaccess │ │ ├── index.php │ │ ├── info.php │ │ ├── main.php │ │ ├── planet.png │ │ └── test │ │ │ └── index.php │ └── star_trek.txt ├── chapter10 │ ├── chap_10_binary_search.php │ ├── chap_10_bubble_sort.php │ ├── chap_10_html_table_multi_array_hidden.php │ ├── chap_10_html_table_multi_array_raw.php │ ├── chap_10_linked_double.php │ ├── chap_10_linked_list_filtered.php │ ├── chap_10_linked_list_in_order.php │ ├── chap_10_linked_list_include.php │ ├── chap_10_oop_using_getters_and_setters.php │ ├── chap_10_oop_using_getters_and_setters_defaults.php │ ├── chap_10_oop_using_getters_and_setters_magic_call.php │ ├── chap_10_search_engine.php │ ├── chap_10_stack.php │ ├── php7cookbook.css │ └── php7cookbook_html_table.css ├── chapter11 │ ├── chap_11_array_to_object.php │ ├── chap_11_mapper.php │ ├── chap_11_object_to_array.php │ ├── chap_11_orm_embedded.php │ ├── chap_11_orm_secondary_lookups.php │ ├── chap_11_parse_tree_structure.php │ ├── chap_11_pub_sub.php │ ├── chap_11_pub_sub_complex_example.php │ ├── chap_11_pub_sub_include.php │ ├── chap_11_pub_sub_simple_example.php │ ├── chap_11_pub_sub_view_logic.php │ ├── chap_11_simple_object_tree_example.php │ ├── chap_11_strategy_pattern.php │ └── php7cookbook.css ├── chapter12 │ ├── .gitignore │ ├── FreeSansBold.ttf │ ├── chap_12_captcha_image.php │ ├── chap_12_captcha_text.php │ ├── chap_12_captcha_view_include.php │ ├── chap_12_form_csrf_test_protected.html │ ├── chap_12_form_csrf_test_unprotected.html │ ├── chap_12_form_protected.php │ ├── chap_12_form_protected_index.php │ ├── chap_12_form_protected_with_token.php │ ├── chap_12_form_protected_with_token.phtml │ ├── chap_12_form_unprotected.php │ ├── chap_12_form_unprotected_include.php │ ├── chap_12_form_view_results.php │ ├── chap_12_openssl_encryption.php │ ├── chap_12_password_generate.php │ ├── chap_12_post_data_filtering.php │ ├── chap_12_post_data_filtering_basic.php │ ├── chap_12_post_data_validation_basic.php │ ├── chap_12_session_hijack.php │ ├── chap_12_session_protected.php │ ├── chap_12_session_protection_simple_login.php │ ├── csrf-test.html │ └── php7cookbook.css ├── chapter13 │ ├── .gitignore │ ├── Demo.php │ ├── SimpleClassTest.php │ ├── SimpleDbTest.php │ ├── SimpleTest.php │ ├── VisitorOps.php │ ├── VisitorOpsMock.php │ ├── VisitorService.php │ ├── VisitorServiceTest.php │ ├── VisitorServiceTestAnonClass.php │ ├── VisitorServiceTestMockBuilder.php │ ├── chap_13_error_handler.php │ ├── chap_13_error_throwable.php │ ├── chap_13_exception_handler.php │ ├── chap_13_fake_data.php │ ├── chap_13_session_options.php │ ├── chap_13_trait_and_interface.php │ ├── chap_13_unit_test_simple.php │ ├── chap_13_unit_test_vistor_ops.php │ ├── composer.json │ ├── composer.lock │ ├── composer.phar │ ├── phpunit.phar │ ├── tests │ │ ├── SimpleClassTest.php │ │ ├── SimpleDbTest.php │ │ ├── SimpleTest.php │ │ ├── VisitorOpsMock.php │ │ ├── VisitorServiceTest.php │ │ └── phpunit.xml │ └── tests_with_autoload │ │ ├── Demo │ │ └── Demo.php │ │ ├── Simple │ │ └── ClassTest.php │ │ ├── bootstrap.php │ │ └── phpunit.xml ├── config │ └── db.config.php └── data │ ├── 6b3628918b77f77ae86001beed72f0ee │ ├── access.log │ ├── db │ └── php7cookbook.db.sqlite │ ├── error.log │ ├── files │ ├── country_code_name.csv │ ├── currencies.csv │ ├── currencyTable.txt │ ├── customer.csv │ ├── customer.sql │ ├── customer_passwords.txt │ ├── first_names.txt │ ├── iso_country_codes.csv │ ├── mongo.db.global.php │ ├── php7cookbook.sql │ ├── prospects.csv │ ├── surnames.txt │ └── war_and_peace.txt │ └── languages │ ├── de_DE │ ├── amsterdam.txt │ ├── bangkok.txt │ ├── main_page.txt │ ├── san_francisco.txt │ ├── translation.csv │ ├── translation.ini │ └── translation.ini~ │ ├── en_GB │ ├── amsterdam.txt │ ├── bangkok.txt │ ├── main_page.txt │ ├── san_francisco.txt │ ├── translation.csv │ ├── translation.ini │ └── translation.ini~ │ ├── es_ES │ ├── amsterdam.txt │ ├── bangkok.txt │ ├── main_page.txt │ ├── san_francisco.txt │ ├── translation.csv │ ├── translation.ini │ └── translation.ini~ │ └── fr_FR │ ├── amsterdam.txt │ ├── bangkok.txt │ ├── main_page.txt │ ├── san_francisco.txt │ ├── translation.csv │ ├── translation.ini │ └── translation.ini~ ├── war_and_peace.txt └── world_city_data.sql.zip /README.md: -------------------------------------------------------------------------------- 1 | # PHP 7 Cookbook 2 | 3 | This repository is designed to supplement the examples discussed in the 4 | [PHP 7 Programming Cookbook](https://www.packtpub.com/application-development/php-7-programming-cookbook) 5 | 6 | ## Database Setup 7 | * Read through Chapter 1, "Defining a test MySQL database" 8 | * See "db.config.php" for example MySQL params; adjust to match your environment 9 | * Use this SQL statement to match the examples in the book: 10 | ``` 11 | CREATE DATABASE IF NOT EXISTS dbname DEFAULT CHARACTER SET utf8 12 | COLLATE utf8_general_ci; 13 | CREATE USER 'cook'@'%' IDENTIFIED WITH mysql_native_password; 14 | SET PASSWORD FOR 'cook'@'%' = PASSWORD('book'); 15 | GRANT ALL PRIVILEGES ON dbname.* to 'cook'@'%'; 16 | GRANT ALL PRIVILEGES ON dbname.* to 'cook'@'localhost'; 17 | FLUSH PRIVILEGES; 18 | ``` 19 | * `php7cookbook.sql` contains all the sample data used in the book 20 | * In order to use the sample data in the `world_city_codes` database table, you'll need to download it separately from this URL: 21 | * opensource.unlikelysource.org/world_city_data.sql.zip 22 | * you can then import `world_city_codes.sql` into the `php7cookbook` database separately 23 | -------------------------------------------------------------------------------- /create_currency_table_db.php: -------------------------------------------------------------------------------- 1 | pdo; 16 | 17 | $fileObj = new SplFileObject(CURRENCY_CSV, 'r'); 18 | $stmt = $pdo->prepare('SELECT * FROM iso_country_codes WHERE name LIKE ?'); 19 | $stmt2 = $pdo->prepare('UPDATE iso_country_codes SET currency_name = ?, currency_code = ?, currency_number = ? WHERE iso2 = ?'); 20 | while ($csvRow = $fileObj->fgetcsv()) { 21 | $search = ucwords(strtolower(substr($csvRow[0], 0, 12))) . '%'; 22 | echo $search . PHP_EOL; 23 | $stmt->execute([$search]); 24 | $dbRow = $stmt->fetch(PDO::FETCH_ASSOC); 25 | if ($dbRow) { 26 | $stmt2->execute([$csvRow[1], $csvRow[2], $csvRow[3], $dbRow['iso2']]); 27 | } 28 | } 29 | echo serialize($currencyTable); 30 | -------------------------------------------------------------------------------- /create_iso_country_codes_csv.php: -------------------------------------------------------------------------------- 1 | pdo; 16 | 17 | // erase current file 18 | file_put_contents(CURRENCY_CSV, ''); 19 | 20 | $fileObj = new SplFileObject(CURRENCY_CSV, 'w'); 21 | $stmt = $pdo->query('SELECT * FROM iso_country_codes'); 22 | while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { 23 | $fileObj->fputcsv($row); 24 | } 25 | unset($fileObj); 26 | readfile(CURRENCY_CSV); 27 | -------------------------------------------------------------------------------- /db.config.php: -------------------------------------------------------------------------------- 1 | 'mysql', 5 | 'host' => 'localhost', 6 | 'dbname' => 'php7cookbook', 7 | 'user' => 'cook', 8 | 'password' => 'book', 9 | 'errmode' => PDO::ERRMODE_EXCEPTION, 10 | ]; 11 | -------------------------------------------------------------------------------- /google_api_key.php: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /phpunit.phar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dbierer/php7cookbook/eda1bf62e38f14502328ac76bb21841c64197ad7/phpunit.phar -------------------------------------------------------------------------------- /source/.buildpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /source/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | PHP 7 Cookbook 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.wst.jsdt.core.javascriptValidator 10 | 11 | 12 | 13 | 14 | org.eclipse.wst.validation.validationbuilder 15 | 16 | 17 | 18 | 19 | org.eclipse.dltk.core.scriptbuilder 20 | 21 | 22 | 23 | 24 | 25 | org.zend.php.framework.ZendFrameworkNature 26 | org.eclipse.php.core.PHPNature 27 | org.eclipse.wst.jsdt.core.jsNature 28 | 29 | 30 | -------------------------------------------------------------------------------- /source/.settings/org.eclipse.php.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | include_path=1;/usr/local/php 3 | phpVersion=php7.0 4 | useShortTags=false 5 | -------------------------------------------------------------------------------- /source/Application/Acl/AuthenticateInterface.php: -------------------------------------------------------------------------------- 1 | connection = $connection; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /source/Application/Database/Search/Criteria.php: -------------------------------------------------------------------------------- 1 | key = $key; 15 | $this->operator = $operator; 16 | $this->item = $item; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /source/Application/Database/Service.php: -------------------------------------------------------------------------------- 1 | connection = $connection; 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /source/Application/Entity/Address.php: -------------------------------------------------------------------------------- 1 | address; 21 | } 22 | 23 | /** 24 | * This method sets the value of $address 25 | * 26 | * @param string $address 27 | * @return address $this 28 | */ 29 | public function setAddress(string $address) 30 | { 31 | $this->address = $address; 32 | return $this; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /source/Application/Entity/Name.php: -------------------------------------------------------------------------------- 1 | name; 21 | } 22 | 23 | /** 24 | * This method sets the value of $name 25 | * 26 | * @param string $name 27 | * @return name $this 28 | */ 29 | public function setName(string $name) 30 | { 31 | $this->name = $name; 32 | return $this; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /source/Application/Entity/Profile.php: -------------------------------------------------------------------------------- 1 | profile; 21 | } 22 | 23 | /** 24 | * This method sets the value of $profile 25 | * 26 | * @param string $profile 27 | * @return profile $this 28 | */ 29 | public function setProfile(string $profile) 30 | { 31 | $this->profile = $profile; 32 | return $this; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /source/Application/Entity/ProtectedPerson.php: -------------------------------------------------------------------------------- 1 | logFile = $logFileDir . '/' . $logFile; 14 | $this->logFile = str_replace('//', '/', $this->logFile); 15 | set_exception_handler([$this,'exceptionHandler']); 16 | set_error_handler([$this, 'errorHandler']); 17 | } 18 | 19 | public function errorHandler($errno, $errstr, $errfile, $errline) 20 | { 21 | $message = sprintf('ERROR : %s : %d : %s : %s : %s' . PHP_EOL, 22 | date('Y-m-d H:i:s'), $errno, $errstr, $errfile, $errline); 23 | file_put_contents($this->logFile, $message, FILE_APPEND); 24 | } 25 | 26 | public function exceptionHandler($ex) 27 | { 28 | $message = sprintf('EXCEPTION: %19s : %20s : %s' . PHP_EOL, 29 | date('Y-m-d H:i:s'), get_class($ex), $ex->getMessage()); 30 | file_put_contents($this->logFile, $message, FILE_APPEND); 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /source/Application/Error/ThrowsError.php: -------------------------------------------------------------------------------- 1 | zero = 1 / 0; 12 | } 13 | public function willNotParse() 14 | { 15 | eval(self::NOT_PARSE); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /source/Application/Error/ThrowsException.php: -------------------------------------------------------------------------------- 1 | $value) { 17 | $dsn .= $key . '=' . $value . ';'; 18 | } 19 | $pdo = new PDO($dsn, 20 | $config['user'], 21 | $config['password'], 22 | [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]); 23 | $stmt = $pdo->query('This Is Not SQL'); 24 | while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { 25 | $this->result[] = $row; 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /source/Application/Event/Listener.php: -------------------------------------------------------------------------------- 1 | listeners[$event][] = $callback; 11 | } 12 | public function trigger($event, $params) 13 | { 14 | if (!isset($this->listeners[$event])) { 15 | return $this->listeners[$this->alias[$event]]($params); 16 | } else { 17 | return $this->listeners[$event]($params); 18 | } 19 | } 20 | public function setAlias($alias, $event) 21 | { 22 | $this->alias[$event] = $alias; 23 | } 24 | } 25 | 26 | -------------------------------------------------------------------------------- /source/Application/Filter/CallbackInterface.php: -------------------------------------------------------------------------------- 1 | pattern = '
' . PHP_EOL; 12 | return sprintf($this->pattern, $this->name, $this->getAttribs()); 13 | } 14 | 15 | public function closeTag() 16 | { 17 | return 'type . '>'; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /source/Application/Generic/CountryList.php: -------------------------------------------------------------------------------- 1 | connection = $connection; 18 | } 19 | public function list() 20 | { 21 | $list = []; 22 | $sql = sprintf('SELECT %s,%s FROM %s', $this->key, $this->value, $this->table); 23 | $stmt = $this->connection->pdo->query($sql); 24 | while ($item = $stmt->fetch(PDO::FETCH_ASSOC)) { 25 | $list[$item[$this->key]] = $item[$this->value]; 26 | } 27 | return $list; 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /source/Application/Generic/CountryListUsingTrait.php: -------------------------------------------------------------------------------- 1 | connection = $connection; 17 | } 18 | public function list() 19 | { 20 | $list = []; 21 | $sql = sprintf('SELECT %s,%s FROM %s', $this->key, $this->value, $this->table); 22 | $stmt = $this->connection->pdo->query($sql); 23 | while ($item = $stmt->fetch(PDO::FETCH_ASSOC)) { 24 | $list[$item[$this->key]] = $item[$this->value]; 25 | } 26 | return $list; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /source/Application/Generic/CustomerListUsingTrait.php: -------------------------------------------------------------------------------- 1 | setConnection(new Connection($dbParams)); 16 | return $class; 17 | } else { 18 | throw new Exception('Unable to initialize this class. Must be Connection Aware'); 19 | } 20 | return FALSE; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /source/Application/Generic/ListTrait.php: -------------------------------------------------------------------------------- 1 | key, $this->value, $this->table); 12 | $stmt = $this->connection->pdo->query($sql); 13 | while ($item = $stmt->fetch(PDO::FETCH_ASSOC)) { 14 | $list[$item[$this->key]] = $item[$this->value]; 15 | } 16 | return $list; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /source/Application/Generic/Node.php: -------------------------------------------------------------------------------- 1 | __set('name', $name); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /source/Application/Generic/Registry.php: -------------------------------------------------------------------------------- 1 | registry[$key] ?? NULL; 23 | } 24 | public function __set($key, $value) 25 | { 26 | $this->registry[$key] = $value; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /source/Application/Generic/Stack.php: -------------------------------------------------------------------------------- 1 | stack = new SplStack(); 12 | } 13 | public function push($message) 14 | { 15 | $this->stack->push($message); 16 | } 17 | public function pop() 18 | { 19 | return $this->stack->pop(); 20 | } 21 | public function __invoke() 22 | { 23 | return $this->stack; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /source/Application/Generic/Temp.php: -------------------------------------------------------------------------------- 1 | name = $name; 36 | $this->address = $address; 37 | $this->profile = $profile; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /source/Application/Generic/Tree.php: -------------------------------------------------------------------------------- 1 | values[$key] = $value; 10 | } 11 | public function __get($key) 12 | { 13 | return $this->values[$key] ?? NULL; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /source/Application/I18n/Day.php: -------------------------------------------------------------------------------- 1 | dayOfMonth = $dayOfMonth; 11 | } 12 | public function __invoke() 13 | { 14 | return $this->dayOfMonth ?? ''; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /source/Application/I18n/IsoCodes.php: -------------------------------------------------------------------------------- 1 | $value) { 18 | $this->$key = $data[$key] ?? NULL; 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /source/Application/I18n/IsoCodesCsv.php: -------------------------------------------------------------------------------- 1 | iso2Column = $iso2Column; 14 | try { 15 | $this->isoCsvFile = new SplFileObject($isoCsvFile, 'r'); 16 | } catch (Exception $e) { 17 | error_log($e->getMessage()); 18 | throw new Exception(self::ERROR_FILE_OPEN); 19 | } 20 | } 21 | public function getCurrencyCodeFromIso2CountryCode($iso2) : IsoCodes 22 | { 23 | $isoCodes = new IsoCodes(); 24 | while ($row = $this->isoCsvFile->fgetcsv()) { 25 | if ($row[$this->iso2Column] == $iso2) { 26 | $var = get_object_vars($isoCodes); 27 | $count = 0; 28 | foreach ($var as $key => $value) { 29 | $isoCodes->$key = $row[$count++]; 30 | } 31 | break; 32 | } 33 | } 34 | return $isoCodes; 35 | } 36 | } 37 | 38 | -------------------------------------------------------------------------------- /source/Application/I18n/IsoCodesDb.php: -------------------------------------------------------------------------------- 1 | connection = $connection; 16 | $this->isoTableName = $isoTableName; 17 | $this->iso2FieldName = $iso2FieldName; 18 | } 19 | public function getCurrencyCodeFromIso2CountryCode($iso2) : IsoCodes 20 | { 21 | $sql = sprintf('SELECT * FROM %s WHERE %s = ?', 22 | $this->isoTableName, 23 | $this->iso2FieldName); 24 | $stmt = $this->connection->pdo->prepare($sql); 25 | $stmt->execute([$iso2]); 26 | $data = $stmt->fetch(PDO::FETCH_ASSOC); 27 | return new IsoCodes($data); 28 | } 29 | } 30 | 31 | -------------------------------------------------------------------------------- /source/Application/I18n/IsoCodesInterface.php: -------------------------------------------------------------------------------- 1 | id = $id; 12 | $this->title = $title; 13 | $this->description = $description; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /source/Application/I18n/Translate/Adapter/Csv.php: -------------------------------------------------------------------------------- 1 | getLocaleCode()); 15 | if (!file_exists($translateFileName)) { 16 | error_log(self::ERROR_NOT_FOUND . ':' . $translateFileName); 17 | throw new Exception(self::ERROR_NOT_FOUND); 18 | } else { 19 | $fileObj = new SplFileObject($translateFileName, 'r'); 20 | while ($row = $fileObj->fgetcsv()) { 21 | $this->translation[$row[0]] = $row[1]; 22 | } 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /source/Application/I18n/Translate/Adapter/Database.php: -------------------------------------------------------------------------------- 1 | defaultLocaleCode = $locale->getLocaleCode(); 17 | $this->connection = $connection; 18 | $sql = 'SELECT msgstr FROM ' . $tableName . ' WHERE locale_code = ? AND msgid = ?'; 19 | $this->statement = $this->connection->pdo->prepare($sql); 20 | } 21 | public function translate($msgid, $localeCode = NULL) 22 | { 23 | if (!$localeCode) $localeCode = $this->defaultLocaleCode; 24 | $this->statement->execute([$localeCode, $msgid]); 25 | return $this->statement->fetchColumn(); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /source/Application/I18n/Translate/Adapter/Ini.php: -------------------------------------------------------------------------------- 1 | getLocaleCode()); 14 | if (!file_exists($translateFileName)) { 15 | error_log(self::ERROR_NOT_FOUND . ':' . $translateFileName); 16 | throw new Exception(self::ERROR_NOT_FOUND); 17 | } else { 18 | $this->translation = parse_ini_file($translateFileName); 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /source/Application/I18n/Translate/Adapter/TranslateAdapterInterface.php: -------------------------------------------------------------------------------- 1 | translation[$msgid] ?? $msgid; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /source/Application/Mvc/Dispatch.php: -------------------------------------------------------------------------------- 1 | config = $config; 20 | $this->vac = new Hoover(); 21 | $this->lang = $_SESSION['lang'][0] ?? 'en'; 22 | } 23 | 24 | public function __invoke($obj, $call, $key) 25 | { 26 | return (new $obj())->$call($this->config[$key], $this); 27 | } 28 | 29 | public function callback($key) 30 | { 31 | return $this->config['callback'][$key]->call($this); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /source/Application/Parse/XmlPath.php: -------------------------------------------------------------------------------- 1 | log = new SplFileObject($filename, 'r'); 22 | } 23 | 24 | public function fileIteratorByLine() 25 | { 26 | $count = 0; 27 | while (!$this->log->eof()) { 28 | yield $this->log->fgets(); 29 | $count++; 30 | } 31 | return $count; 32 | } 33 | 34 | public function getIp($line) 35 | { 36 | preg_match('/(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/', $line, $match); 37 | return $match[1] ?? ''; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /source/Application/Web/Menu/Item.php: -------------------------------------------------------------------------------- 1 | uri = $uri; 23 | $this->method = $method; 24 | $this->headers = $headers; 25 | $this->data = $data; 26 | $this->cookies = $cookies; 27 | $this->setTransport(); 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /source/Application/Web/Rest/AbstractApi.php: -------------------------------------------------------------------------------- 1 | registeredKeys = $registeredKeys; 25 | } 26 | public static function generateToken() 27 | { 28 | return bin2hex(random_bytes(self::TOKEN_BYTE_SIZE)); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /source/Application/Web/Rest/ApiInterface.php: -------------------------------------------------------------------------------- 1 | =5.3.0" 14 | }, 15 | "autoload": { 16 | "psr-4": { 17 | "Psr\\Http\\Message\\": "src/" 18 | } 19 | }, 20 | "extra": { 21 | "branch-alias": { 22 | "dev-master": "1.0.x-dev" 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /source/chapter01/chap_01_autoload_test.php: -------------------------------------------------------------------------------- 1 | getTest(); 13 | 14 | // get "fake" class (which doesn't exist) 15 | // uncomment the 2 lines below to see results 16 | //$fake = new Application\Test\FakeClass(); 17 | //echo $fake->getTest(); 18 | -------------------------------------------------------------------------------- /source/chapter01/chap_01_php5_to_php7_code_converter.php: -------------------------------------------------------------------------------- 1 | ' . PHP_EOL; 11 | exit; 12 | } 13 | 14 | // setup class autoloading 15 | require __DIR__ . '/../Application/Autoload/Loader.php'; 16 | 17 | // add current directory to the path 18 | Application\Autoload\Loader::init(__DIR__ . '/..'); 19 | 20 | // get "deep scan" class 21 | $convert = new Application\Parse\Convert(); 22 | echo $convert->scan($filename); 23 | echo PHP_EOL; 24 | -------------------------------------------------------------------------------- /source/chapter01/chap_01_php5_to_php7_code_converter_test_file.php: -------------------------------------------------------------------------------- 1 | <% 2 | date_default_timezone_set('Europe/Berlin'); 3 | function myErrorHandler($errno, $errstr, $errfile, $errline) 4 | { 5 | // some code 6 | } 7 | $old_error_handler = set_error_handler("myErrorHandler"); 8 | 9 | $php5 = 'Works in PHP 5'; 10 | $foo = ['bar' => ['baz' => 'php5']]; 11 | echo $$foo['bar']['baz']; 12 | echo PHP_EOL; 13 | 14 | list() = $a; 15 | 16 | echo '\u{xxx} has a special meaning in PHP 7'; 17 | echo PHP_EOL; 18 | 19 | var_dump(1 << -1); 20 | echo PHP_EOL; 21 | var_dump(1 >> 65); 22 | echo PHP_EOL; 23 | 24 | $now = new DateTime(); 25 | call_user_method_array('setDate', $now, array(2016, 11, 11)); 26 | call_user_method_array('setTime', $now, array(11, 11, 11)); 27 | echo call_user_method('format', $now, 'Y-m-d H:i:s'); 28 | echo PHP_EOL; 29 | 30 | $ids = 'Alice = 1, Dick = 33, Jane = 102, Fred = 88'; 31 | echo preg_replace('/([0-9]{1,3})/e','sprintf("%03d",$1)',$ids); 32 | echo PHP_EOL; 33 | 34 | %> 35 | -------------------------------------------------------------------------------- /source/chapter01/chap_01_php7_to_php5_code_converter.php: -------------------------------------------------------------------------------- 1 | ' . PHP_EOL; 11 | exit; 12 | } 13 | 14 | // setup class autoloading 15 | require __DIR__ . '/../Application/Autoload/Loader.php'; 16 | 17 | // add current directory to the path 18 | Application\Autoload\Loader::init(__DIR__ . '/..'); 19 | 20 | // get "deep scan" class 21 | $convert = new Application\Parse\Convert(); 22 | echo $convert->scan($filename); 23 | echo PHP_EOL; 24 | -------------------------------------------------------------------------------- /source/chapter01/chap_01_php7_to_php5_code_converter_test_file.php: -------------------------------------------------------------------------------- 1 | <% 2 | date_default_timezone_set('Europe/Berlin'); 3 | function myErrorHandler($errno, $errstr, $errfile, $errline) 4 | { 5 | // some code 6 | } 7 | $old_error_handler = set_error_handler("myErrorHandler"); 8 | 9 | $php5 = 'Works in PHP 5'; 10 | $foo = ['bar' => ['baz' => 'php5']]; 11 | echo $$foo['bar']['baz']; 12 | echo PHP_EOL; 13 | 14 | list() = $a; 15 | 16 | echo '\u{xxx} has a special meaning in PHP 7'; 17 | echo PHP_EOL; 18 | 19 | var_dump(1 << -1); 20 | echo PHP_EOL; 21 | var_dump(1 >> 65); 22 | echo PHP_EOL; 23 | 24 | $now = new DateTime(); 25 | call_user_method_array('setDate', $now, array(2016, 11, 11)); 26 | call_user_method_array('setTime', $now, array(11, 11, 11)); 27 | echo call_user_method('format', $now, 'Y-m-d H:i:s'); 28 | echo PHP_EOL; 29 | 30 | $ids = 'Alice = 1, Dick = 33, Jane = 102, Fred = 88'; 31 | echo preg_replace('/([0-9]{1,3})/e','sprintf("%03d",$1)',$ids); 32 | echo PHP_EOL; 33 | 34 | %> 35 | -------------------------------------------------------------------------------- /source/chapter01/chap_01_vacuuming_website.php: -------------------------------------------------------------------------------- 1 | getTags($url, $tag)); 25 | -------------------------------------------------------------------------------- /source/chapter01/test.php: -------------------------------------------------------------------------------- 1 | getTags($url, $tag)); 25 | -------------------------------------------------------------------------------- /source/chapter02/chap_02_foreach.php: -------------------------------------------------------------------------------- 1 | getIterator('ByLine'); 17 | // NOTE: this comes back as an instance of Generator, which implements Iterator 18 | echo get_class($iterator); 19 | // iterate through large file; count number of words per line and record average 20 | $words = 0; 21 | foreach ($iterator as $line) { 22 | echo $line; 23 | $words += str_word_count($line); 24 | } 25 | echo str_repeat('-', 52) . PHP_EOL; 26 | printf("%-40s : %8d\n", 'Total Words', $words); 27 | printf("%-40s : %8d\n", 'Average Words Per Line', ($words / $iterator->getReturn())); 28 | echo str_repeat('-', 52) . PHP_EOL; 29 | } catch (Throwable $e) { 30 | echo $e->getMessage(); 31 | } 32 | -------------------------------------------------------------------------------- /source/chapter02/chap_02_recursive_directory_iterator.php: -------------------------------------------------------------------------------- 1 | ls('*.php') as $info) { 18 | echo $info; 19 | } 20 | 21 | echo 'Mimics "dir /s" ' . PHP_EOL; 22 | foreach ($directory->dir('*.php') as $info) { 23 | echo $info; 24 | } 25 | 26 | } catch (Throwable $e) { 27 | echo $e->getMessage(); 28 | } 29 | -------------------------------------------------------------------------------- /source/chapter02/chap_02_simple_ast_example.php: -------------------------------------------------------------------------------- 1 | function () { return [ 8 | 1 => function ($a) { return 'Level 1/1:' . ++$a; }, 9 | 2 => function ($a) { return 'Level 1/2:' . ++$a; }, 10 | ];}, 11 | 2 => function () { return [ 12 | 1 => function ($a) { return 'Level 2/1:' . ++$a; }, 13 | 2 => function ($a) { return 'Level 2/2:' . ++$a; }, 14 | ];} 15 | ]; 16 | } 17 | 18 | $a = 't'; 19 | $t = 'test'; 20 | 21 | // outputs: Level 1/2:101 22 | // NOTE: PHP 5 returns a parse error! 23 | echo $$a()[1]()[2](100); 24 | 25 | -------------------------------------------------------------------------------- /source/chapter02/chap_02_understanding_ast_simple_example.php: -------------------------------------------------------------------------------- 1 | grand = new Node('GrandPa'); 14 | $tree->grand->father = new Node('Father'); 15 | $tree->grand->father->son = new Node('Son'); 16 | var_dump($tree->grand, $tree->grand->father, $tree->grand->father->son); 17 | ?> 18 | 19 | 20 | PHP 7 Cookbook 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /source/chapter02/chap_02_understanding_variable_variables.php: -------------------------------------------------------------------------------- 1 | 7 | 8 | 9 | PHP 7 Cookbook 10 | 11 | 12 | 13 | 14 | 19 | 20 |
21 | 22 | 23 |
24 |

Current

25 |
26 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /source/chapter02/images/arrow-2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dbierer/php7cookbook/eda1bf62e38f14502328ac76bb21841c64197ad7/source/chapter02/images/arrow-2.png -------------------------------------------------------------------------------- /source/chapter02/images/arrow1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dbierer/php7cookbook/eda1bf62e38f14502328ac76bb21841c64197ad7/source/chapter02/images/arrow1.png -------------------------------------------------------------------------------- /source/chapter02/images/brand-logo1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dbierer/php7cookbook/eda1bf62e38f14502328ac76bb21841c64197ad7/source/chapter02/images/brand-logo1.png -------------------------------------------------------------------------------- /source/chapter02/images/brand-logo2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dbierer/php7cookbook/eda1bf62e38f14502328ac76bb21841c64197ad7/source/chapter02/images/brand-logo2.png -------------------------------------------------------------------------------- /source/chapter02/images/brand-logo3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dbierer/php7cookbook/eda1bf62e38f14502328ac76bb21841c64197ad7/source/chapter02/images/brand-logo3.png -------------------------------------------------------------------------------- /source/chapter02/images/brand-logo4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dbierer/php7cookbook/eda1bf62e38f14502328ac76bb21841c64197ad7/source/chapter02/images/brand-logo4.png -------------------------------------------------------------------------------- /source/chapter02/images/btn-icons.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dbierer/php7cookbook/eda1bf62e38f14502328ac76bb21841c64197ad7/source/chapter02/images/btn-icons.png -------------------------------------------------------------------------------- /source/chapter02/images/divice-half.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dbierer/php7cookbook/eda1bf62e38f14502328ac76bb21841c64197ad7/source/chapter02/images/divice-half.png -------------------------------------------------------------------------------- /source/chapter02/images/divice-in-hand.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dbierer/php7cookbook/eda1bf62e38f14502328ac76bb21841c64197ad7/source/chapter02/images/divice-in-hand.png -------------------------------------------------------------------------------- /source/chapter02/images/divice.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dbierer/php7cookbook/eda1bf62e38f14502328ac76bb21841c64197ad7/source/chapter02/images/divice.png -------------------------------------------------------------------------------- /source/chapter02/images/fav.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dbierer/php7cookbook/eda1bf62e38f14502328ac76bb21841c64197ad7/source/chapter02/images/fav.png -------------------------------------------------------------------------------- /source/chapter02/images/feature-icons-l.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dbierer/php7cookbook/eda1bf62e38f14502328ac76bb21841c64197ad7/source/chapter02/images/feature-icons-l.png -------------------------------------------------------------------------------- /source/chapter02/images/feature-icons-r.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dbierer/php7cookbook/eda1bf62e38f14502328ac76bb21841c64197ad7/source/chapter02/images/feature-icons-r.png -------------------------------------------------------------------------------- /source/chapter02/images/heart-icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dbierer/php7cookbook/eda1bf62e38f14502328ac76bb21841c64197ad7/source/chapter02/images/heart-icon.png -------------------------------------------------------------------------------- /source/chapter02/images/icon1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dbierer/php7cookbook/eda1bf62e38f14502328ac76bb21841c64197ad7/source/chapter02/images/icon1.png -------------------------------------------------------------------------------- /source/chapter02/images/logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dbierer/php7cookbook/eda1bf62e38f14502328ac76bb21841c64197ad7/source/chapter02/images/logo.png -------------------------------------------------------------------------------- /source/chapter02/images/msg-icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dbierer/php7cookbook/eda1bf62e38f14502328ac76bb21841c64197ad7/source/chapter02/images/msg-icon.png -------------------------------------------------------------------------------- /source/chapter02/images/nav-icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dbierer/php7cookbook/eda1bf62e38f14502328ac76bb21841c64197ad7/source/chapter02/images/nav-icon.png -------------------------------------------------------------------------------- /source/chapter02/images/play-btn.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dbierer/php7cookbook/eda1bf62e38f14502328ac76bb21841c64197ad7/source/chapter02/images/play-btn.png -------------------------------------------------------------------------------- /source/chapter02/images/quit-people.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dbierer/php7cookbook/eda1bf62e38f14502328ac76bb21841c64197ad7/source/chapter02/images/quit-people.jpg -------------------------------------------------------------------------------- /source/chapter02/images/screen-coursuals.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dbierer/php7cookbook/eda1bf62e38f14502328ac76bb21841c64197ad7/source/chapter02/images/screen-coursuals.png -------------------------------------------------------------------------------- /source/chapter02/images/screen.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dbierer/php7cookbook/eda1bf62e38f14502328ac76bb21841c64197ad7/source/chapter02/images/screen.png -------------------------------------------------------------------------------- /source/chapter02/images/slide.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dbierer/php7cookbook/eda1bf62e38f14502328ac76bb21841c64197ad7/source/chapter02/images/slide.jpg -------------------------------------------------------------------------------- /source/chapter02/images/social-icons.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dbierer/php7cookbook/eda1bf62e38f14502328ac76bb21841c64197ad7/source/chapter02/images/social-icons.png -------------------------------------------------------------------------------- /source/chapter02/images/team-member.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dbierer/php7cookbook/eda1bf62e38f14502328ac76bb21841c64197ad7/source/chapter02/images/team-member.jpg -------------------------------------------------------------------------------- /source/chapter02/images/team-member1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dbierer/php7cookbook/eda1bf62e38f14502328ac76bb21841c64197ad7/source/chapter02/images/team-member1.jpg -------------------------------------------------------------------------------- /source/chapter02/images/team-member2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dbierer/php7cookbook/eda1bf62e38f14502328ac76bb21841c64197ad7/source/chapter02/images/team-member2.jpg -------------------------------------------------------------------------------- /source/chapter02/images/team-member3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dbierer/php7cookbook/eda1bf62e38f14502328ac76bb21841c64197ad7/source/chapter02/images/team-member3.jpg -------------------------------------------------------------------------------- /source/chapter02/images/team-member4.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dbierer/php7cookbook/eda1bf62e38f14502328ac76bb21841c64197ad7/source/chapter02/images/team-member4.jpg -------------------------------------------------------------------------------- /source/chapter02/images/team-social.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dbierer/php7cookbook/eda1bf62e38f14502328ac76bb21841c64197ad7/source/chapter02/images/team-social.png -------------------------------------------------------------------------------- /source/chapter02/images/testmonials.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dbierer/php7cookbook/eda1bf62e38f14502328ac76bb21841c64197ad7/source/chapter02/images/testmonials.png -------------------------------------------------------------------------------- /source/chapter02/images/to-top1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dbierer/php7cookbook/eda1bf62e38f14502328ac76bb21841c64197ad7/source/chapter02/images/to-top1.png -------------------------------------------------------------------------------- /source/chapter02/images/top-slider-pagenate.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dbierer/php7cookbook/eda1bf62e38f14502328ac76bb21841c64197ad7/source/chapter02/images/top-slider-pagenate.png -------------------------------------------------------------------------------- /source/chapter02/w3layouts-license.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dbierer/php7cookbook/eda1bf62e38f14502328ac76bb21841c64197ad7/source/chapter02/w3layouts-license.txt -------------------------------------------------------------------------------- /source/chapter03/chap_03_developing_functions_array_iterator.php: -------------------------------------------------------------------------------- 1 | 10 | 11 | 12 | PHP 7 Cookbook 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /source/chapter03/chap_03_developing_functions_iterators.php: -------------------------------------------------------------------------------- 1 | getMessage(); 31 | } 32 | 33 | -------------------------------------------------------------------------------- /source/chapter03/chap_03_developing_functions_namespace.php: -------------------------------------------------------------------------------- 1 | getMessage(); 13 | echo PHP_EOL; 14 | } 15 | 16 | -------------------------------------------------------------------------------- /source/chapter03/chap_03_developing_functions_namespace_alpha.php: -------------------------------------------------------------------------------- 1 | getMessage(); 31 | } 32 | 33 | -------------------------------------------------------------------------------- /source/chapter03/chap_03_developing_functions_return_types_library.php: -------------------------------------------------------------------------------- 1 | format($format); 12 | } 13 | 14 | // converts value to return type of string 15 | function convertsToString($a, $b, $c) : string 16 | { 17 | return $a + $b + $c; 18 | } 19 | 20 | // returns DateTime 21 | // NOTE: this would be a great place for scalar type hints 22 | // if $year, $month or $day are not type int, a warning is generated 23 | function makesDateTime($year, $month, $day) : DateTime 24 | { 25 | $date = new DateTime(); 26 | $date->setDate($year, $month, $day); 27 | return $date; 28 | } 29 | 30 | // this will throw a TypeError at runtime 31 | function wrongDateTime($year, $month, $day) : DateTime 32 | { 33 | return date($year . '-' . $month . '-' . $day); 34 | } 35 | -------------------------------------------------------------------------------- /source/chapter03/chap_03_developing_functions_stacked_iterators.php: -------------------------------------------------------------------------------- 1 | current(); 25 | // returns letters with "even" ASCII codes 26 | return !(ord($current) & 1); 27 | } 28 | }; 29 | echo "\nArrayIterator + FilterIterator\n"; 30 | showElements($f); 31 | 32 | // just using ArrayIterator + FilterIterator + LimitIterator 33 | // F H J L N P 34 | $l = new LimitIterator($f, 2, 6); // $f = iterator, 2 = offset, 6 = length 35 | echo "\nArrayIterator + FilterIterator + LimitIterator\n"; 36 | showElements($l); 37 | 38 | -------------------------------------------------------------------------------- /source/chapter03/test.php: -------------------------------------------------------------------------------- 1 | getMessage(); 25 | } 26 | -------------------------------------------------------------------------------- /source/chapter04/Test.php: -------------------------------------------------------------------------------- 1 | test; 8 | } 9 | } 10 | 11 | class test 12 | { 13 | protected $test = 'TEST'; 14 | public function getTest() 15 | { 16 | return $this->test; 17 | } 18 | } 19 | 20 | $t = new Test(); 21 | echo $t->getTest(); 22 | 23 | -------------------------------------------------------------------------------- /source/chapter04/TwoClass.php: -------------------------------------------------------------------------------- 1 | instance) { 16 | $this->instance = new self(); 17 | } 18 | return $this->instance; 19 | } 20 | public function __get($key) 21 | { 22 | return $this->registry[$key] ?? NULL; 23 | } 24 | public function __set($key, $value) 25 | { 26 | $this->registry[$key] = $value; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /source/chapter04/chap_04_oop_anonymous_class_extends.php: -------------------------------------------------------------------------------- 1 | limit = $limit; 11 | parent::__construct($iterator); 12 | } 13 | public function accept() 14 | { 15 | return ($this->current() <= $this->limit); 16 | } 17 | }; 18 | 19 | echo "\nAnonymous Class Extends FilterIterator\n"; 20 | foreach ($f as $item) echo $item . ' '; 21 | echo PHP_EOL; 22 | 23 | -------------------------------------------------------------------------------- /source/chapter04/chap_04_oop_classes_case_insensitive.php: -------------------------------------------------------------------------------- 1 | getTest(); 8 | echo PHP_EOL; 9 | 10 | $test->setTest('ABC'); 11 | echo $test->getTest(); 12 | echo PHP_EOL; 13 | -------------------------------------------------------------------------------- /source/chapter04/chap_04_oop_extends.php: -------------------------------------------------------------------------------- 1 | id; 10 | } 11 | public function setId($id) 12 | { 13 | $this->id = $id; 14 | } 15 | } 16 | 17 | class Customer extends Base 18 | { 19 | protected $name; 20 | public function getName() 21 | { 22 | return $this->name; 23 | } 24 | public function setName($name) 25 | { 26 | $this->name = $name; 27 | } 28 | } 29 | 30 | $customer = new Customer(); 31 | $customer->setId(100); 32 | $customer->setName('Fred'); 33 | var_dump($customer); 34 | -------------------------------------------------------------------------------- /source/chapter04/chap_04_oop_factory_pattern_example.php: -------------------------------------------------------------------------------- 1 | query('SELECT name FROM iso_country_codes'); 9 | while ($country = $stmt->fetch(PDO::FETCH_COLUMN)) echo $country . ' '; 10 | -------------------------------------------------------------------------------- /source/chapter04/chap_04_oop_interfaces_collisions.php: -------------------------------------------------------------------------------- 1 | date = $date; 26 | } 27 | public function setTime($time) 28 | { 29 | $this->time = $time; 30 | } 31 | public function setBoth(DateTime $dateTime) 32 | { 33 | $this->date = $date; 34 | } 35 | } 36 | 37 | -------------------------------------------------------------------------------- /source/chapter04/chap_04_oop_late_static_binding_illustration.php: -------------------------------------------------------------------------------- 1 | param1 = 12345; 10 | $registry->param2 = 'ABC'; 11 | var_dump($registry); 12 | 13 | // the next few lines will not work 14 | echo PHP_EOL; 15 | try { 16 | $registry = new Registry(); 17 | $registry->param1 = 12345; 18 | $registry->param2 = 'ABC'; 19 | var_dump($registry); 20 | } catch (Error $e) { 21 | echo $e->getMessage(); 22 | } 23 | 24 | -------------------------------------------------------------------------------- /source/chapter04/chap_04_oop_simple_interfaces_example.php: -------------------------------------------------------------------------------- 1 | list() as $item) echo $item . ' '; 24 | echo "\n\n\n\n"; 25 | 26 | // build list customer class 27 | echo "\n-------------\n"; 28 | echo "Customer List\n"; 29 | echo "-------------\n"; 30 | $list = Application\Generic\ListFactory:: 31 | factory(new Application\Generic\CustomerListUsingTrait(), $params); 32 | foreach ($list->list() as $item) echo $item . ' '; 33 | 34 | } catch (Throwable $e) { 35 | echo $e->getMessage(); 36 | } 37 | -------------------------------------------------------------------------------- /source/chapter04/chap_04_oop_simple_static.php: -------------------------------------------------------------------------------- 1 | id; 10 | } 11 | public function setId($id) 12 | { 13 | $this->id = $id; 14 | } 15 | } 16 | 17 | class Customer extends Base 18 | { 19 | protected $name; 20 | public function getName() 21 | { 22 | return $this->name; 23 | } 24 | public function setName($name) 25 | { 26 | $this->name = $name; 27 | } 28 | } 29 | 30 | class Member extends Customer 31 | { 32 | protected $membership; 33 | public function getMembership() 34 | { 35 | return $this->membership; 36 | } 37 | public function setMembership($memberId) 38 | { 39 | $this->membership = $memberId; 40 | } 41 | } 42 | 43 | $member = new Member(); 44 | $member->setId(100); 45 | $member->setName('Fred'); 46 | $member->setMembership('A299F322'); 47 | var_dump($member); 48 | -------------------------------------------------------------------------------- /source/chapter04/chap_04_oop_trait_anonymous_class.php: -------------------------------------------------------------------------------- 1 | id = $id; 12 | } 13 | public function setKey() 14 | { 15 | $this->key = date('YmdHis') . sprintf('%04d', rand(0,9999)); 16 | } 17 | } 18 | 19 | trait NameTrait 20 | { 21 | protected $name; 22 | public $key; 23 | public function setName($name) 24 | { 25 | $this->name = $name; 26 | } 27 | public function setKey() 28 | { 29 | $this->key = unpack('H*', random_bytes(18))[1]; 30 | } 31 | } 32 | 33 | // uses setKey() from NameTrait 34 | $a = new class() { 35 | use IdTrait, NameTrait { 36 | NameTrait::setKey insteadof IdTrait; 37 | IdTrait::setKey as setKeyDate; 38 | } 39 | }; 40 | 41 | $a->setId(100); 42 | $a->setName('Fred'); 43 | $a->setKey(); 44 | var_dump($a); 45 | 46 | $a->setKeyDate(); 47 | var_dump($a); 48 | -------------------------------------------------------------------------------- /source/chapter04/chap_04_oop_trait_methods_do_not_override_class_methods.php: -------------------------------------------------------------------------------- 1 | id = $id; 11 | $this->id = $obj; 12 | } 13 | public function getId() 14 | { 15 | return $this->id; 16 | } 17 | } 18 | 19 | class Customer 20 | { 21 | use Test; 22 | protected $name; 23 | public function setId($id) 24 | { 25 | $this->id = $id; 26 | } 27 | public function getName() 28 | { 29 | return $this->name; 30 | } 31 | public function setName($name) 32 | { 33 | $this->name = $name; 34 | } 35 | } 36 | 37 | $customer = new Customer(); 38 | $customer->setId(100); 39 | $customer->setName('Fred'); 40 | var_dump($customer); 41 | -------------------------------------------------------------------------------- /source/chapter04/chap_04_oop_trait_multiple.php: -------------------------------------------------------------------------------- 1 | id = $id; 12 | } 13 | public function setKey() 14 | { 15 | $this->key = date('YmdHis') . sprintf('%04d', rand(0,9999)); 16 | } 17 | } 18 | 19 | trait NameTrait 20 | { 21 | protected $name; 22 | public $key; 23 | public function setName($name) 24 | { 25 | $this->name = $name; 26 | } 27 | public function setKey() 28 | { 29 | $this->key = unpack('H*', random_bytes(18))[1]; 30 | } 31 | } 32 | 33 | // uses setKey() from NameTrait 34 | class Test 35 | { 36 | use IdTrait, NameTrait { 37 | NameTrait::setKey insteadof IdTrait; 38 | IdTrait::setKey as setKeyDate; 39 | } 40 | } 41 | 42 | $a = new Test(); 43 | $a->setId(100); 44 | $a->setName('Fred'); 45 | $a->setKey(); 46 | var_dump($a); 47 | 48 | $a->setKeyDate(); 49 | var_dump($a); 50 | -------------------------------------------------------------------------------- /source/chapter04/chap_04_oop_traits_override_inherited.php: -------------------------------------------------------------------------------- 1 | id = $id; 10 | $this->id = $obj; 11 | } 12 | } 13 | 14 | class Base 15 | { 16 | protected $id; 17 | public function getId() 18 | { 19 | return $this->id; 20 | } 21 | public function setId($id) 22 | { 23 | $this->id = $id; 24 | } 25 | } 26 | 27 | class Customer extends Base 28 | { 29 | use Test; 30 | protected $name; 31 | public function getName() 32 | { 33 | return $this->name; 34 | } 35 | public function setName($name) 36 | { 37 | $this->name = $name; 38 | } 39 | } 40 | 41 | $customer = new Customer(); 42 | $customer->setId(100); 43 | $customer->setName('Fred'); 44 | var_dump($customer); 45 | -------------------------------------------------------------------------------- /source/chapter04/chap_04_oop_two_classes_in_one_file.php: -------------------------------------------------------------------------------- 1 | setName('TEST'); 8 | $addr = new Address(); 9 | $addr->setAddress('123 Main Street'); 10 | 11 | echo $name->getName() . ' lives at ' . $addr->getAddress(); 12 | -------------------------------------------------------------------------------- /source/chapter04/chap_04_oop_using_stdclass.php: -------------------------------------------------------------------------------- 1 | test = 'TEST'; 9 | echo $obj->test; 10 | echo PHP_EOL; 11 | 12 | // demonstrates a return value of stdClass from a PDO database query 13 | 14 | // set up database connection 15 | include (__DIR__ . '/../Application/Database/Connection.php'); 16 | $connection = new Application\Database\Connection(include __DIR__ . DB_CONFIG_FILE); 17 | 18 | // PDO query which outputs instances of stdClass 19 | $sql = 'SELECT * FROM iso_country_codes'; 20 | $stmt = $connection->pdo->query($sql); 21 | $row = $stmt->fetch(PDO::FETCH_OBJ); 22 | var_dump($row); 23 | 24 | /* 25 | * output: 26 | TEST 27 | class stdClass#5 (5) { 28 | public $name => 29 | string(11) "Afghanistan" 30 | public $iso2 => 31 | string(2) "AF" 32 | public $iso3 => 33 | string(3) "AFG" 34 | public $iso_numeric => 35 | string(1) "4" 36 | public $iso_3166 => 37 | string(13) "ISO 3166-2:AF" 38 | } 39 | */ 40 | -------------------------------------------------------------------------------- /source/chapter05/chap_05_entity_to_query_fetch_by_id.php: -------------------------------------------------------------------------------- 1 | fetchById(rand(1,79))); 21 | 22 | echo "\nMultiple Results\n"; 23 | printf('%4s | %20s | %5s' . PHP_EOL, 'ID', 'NAME', 'LEVEL'); 24 | printf('%4s | %20s | %5s' . PHP_EOL, '----', str_repeat('-', 20), '-----'); 25 | foreach ($service->fetchByLevel('ADV') as $cust) { 26 | printf('%4d | %20s | %5s' . PHP_EOL, $cust->getId(), $cust->getName(), $cust->getLevel()); 27 | } 28 | -------------------------------------------------------------------------------- /source/chapter05/chap_05_jquery_datatables_php_lookups_ajax.php: -------------------------------------------------------------------------------- 1 | pdo->prepare($sql); 17 | $stmt->execute(['id' => (int) $id]); 18 | $results = array(); 19 | while ($row = $stmt->fetch(PDO::FETCH_NUM)) { 20 | $results[] = $row; 21 | } 22 | echo json_encode(['data' => $results]); 23 | -------------------------------------------------------------------------------- /source/chapter05/chap_05_match_entity_to_table.php: -------------------------------------------------------------------------------- 1 | pdo->prepare('SELECT * FROM customer WHERE id = :id'); 22 | $stmt->execute(['id' => $id]); 23 | $result = $stmt->fetch(PDO::FETCH_ASSOC); 24 | 25 | // create Customer entity from data 26 | $cust = Customer::arrayToEntity($result, new Customer()); 27 | var_dump($cust); 28 | -------------------------------------------------------------------------------- /source/chapter05/chap_05_oop_query_builder.php: -------------------------------------------------------------------------------- 1 | where() 15 | ->like('name', '%secret%') 16 | ->and('priority > 9') 17 | ->or('code')->in(['4', '5', '7']) 18 | ->and()->not('created_at') 19 | ->limit(10) 20 | ->offset(20); 21 | 22 | echo Finder::getSql(); 23 | -------------------------------------------------------------------------------- /source/chapter05/chap_05_pdo_connect_mysql.php: -------------------------------------------------------------------------------- 1 | 'localhost', 6 | 'user' => 'test', 7 | 'pwd' => 'password', 8 | 'db' => 'php7cookbook' 9 | ]; 10 | 11 | try { 12 | $dsn = sprintf('mysql:charset=UTF8;host=%s;dbname=%s', $params['host'], $params['db']); 13 | $pdo = new PDO($dsn, $params['user'], $params['pwd']); 14 | $stmt = $pdo->query('SELECT * FROM customer ORDER BY id LIMIT 20'); 15 | printf('%4s | %20s | %5s | %7s' . PHP_EOL, 'ID', 'NAME', 'LEVEL', 'BALANCE'); 16 | printf('%4s | %20s | %5s | %7s' . PHP_EOL, '----', str_repeat('-', 20), '-----', '-------'); 17 | while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { 18 | printf('%4d | %20s | %5s | %7.2f' . PHP_EOL, 19 | $row['id'], $row['name'], $row['level'], $row['balance']); 20 | } 21 | } catch (PDOException $e) { 22 | error_log($e->getMessage()); 23 | } catch (Throwable $e) { 24 | error_log($e->getMessage()); 25 | } 26 | -------------------------------------------------------------------------------- /source/chapter05/chap_05_pdo_connect_mysql_extended_dsn.php: -------------------------------------------------------------------------------- 1 | 'localhost', 6 | 'user' => 'test', 7 | 'pwd' => 'password', 8 | 'db' => 'php7cookbook', 9 | 'sock' => '/var/run/mysqld/mysqld.sock' 10 | ]; 11 | 12 | try { 13 | $dsn = sprintf('mysql:charset=UTF8;host=%s;dbname=%s;unix_socket=%s', 14 | $params['host'], $params['db'], $params['sock']); 15 | $opts = [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]; 16 | $pdo = new PDO($dsn, $params['user'], $params['pwd'], $opts); 17 | $stmt = $pdo->query('SELECT * FROM customer ORDER BY id LIMIT 20'); 18 | printf('%4s | %20s | %5s' . PHP_EOL, 'ID', 'NAME', 'LEVEL'); 19 | printf('%4s | %20s | %5s' . PHP_EOL, '----', str_repeat('-', 20), '-----'); 20 | while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { 21 | printf('%4d | %20s | %5s' . PHP_EOL, $row['id'], $row['name'], $row['level']); 22 | } 23 | } catch (PDOException $e) { 24 | echo $e->getMessage(); 25 | } catch (Throwable $e) { 26 | echo $e->getMessage(); 27 | } 28 | -------------------------------------------------------------------------------- /source/chapter05/chap_05_pdo_connect_pgsql.php: -------------------------------------------------------------------------------- 1 | 'localhost', 6 | 'user' => 'test', 7 | 'pwd' => 'password', 8 | 'db' => 'php7cookbook' 9 | ]; 10 | 11 | try { 12 | $dsn = sprintf('pgsql:host=%s;dbname=%s;user=%s;password=%s', 13 | $params['host'], 14 | $params['db'], 15 | $params['user'], 16 | $params['pwd']); 17 | $pdo = new PDO($dsn); 18 | $stmt = $pdo->query('SELECT * FROM customer ORDER BY id LIMIT 20'); 19 | printf('%4s | %20s | %5s | %7s' . PHP_EOL, 'ID', 'NAME', 'LEVEL', 'BALANCE'); 20 | printf('%4s | %20s | %5s | %7s' . PHP_EOL, '----', str_repeat('-', 20), '-----', '-------'); 21 | while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { 22 | printf('%4d | %20s | %5s | %7.2f' . PHP_EOL, 23 | $row['id'], $row['name'], $row['level'], $row['balance']); 24 | } 25 | } catch (PDOException $e) { 26 | echo $e->getMessage(); 27 | } catch (Throwable $e) { 28 | echo $e->getMessage(); 29 | } 30 | -------------------------------------------------------------------------------- /source/chapter05/chap_05_pdo_connect_sqlite.php: -------------------------------------------------------------------------------- 1 | NULL, 6 | 'pwd' => NULL, 7 | 'db' => __DIR__ . '/../data/db/php7cookbook.db.sqlite' 8 | ]; 9 | 10 | try { 11 | $dsn = sprintf('sqlite:' . $params['db']); 12 | $opts = [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]; 13 | // no username or password for sqlite, thus NULL x 2 14 | $pdo = new PDO($dsn, $params['user'], $params['pwd'], $opts); 15 | $stmt = $pdo->query('SELECT * FROM customer ORDER BY id LIMIT 20'); 16 | printf('%4s | %20s | %5s | %7s' . PHP_EOL, 'ID', 'NAME', 'LEVEL', 'BALANCE'); 17 | printf('%4s | %20s | %5s | %7s' . PHP_EOL, '----', str_repeat('-', 20), '-----', '-------'); 18 | while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { 19 | printf('%4d | %20s | %5s | %7.2f' . PHP_EOL, 20 | $row['id'], $row['name'], $row['level'], $row['balance']); 21 | } 22 | } catch (PDOException $e) { 23 | echo $e->getMessage(); 24 | } catch (Throwable $e) { 25 | echo $e->getMessage(); 26 | } 27 | 28 | -------------------------------------------------------------------------------- /source/chapter05/chap_05_pdo_mysql_fetch_class.php: -------------------------------------------------------------------------------- 1 | 'localhost', 14 | 'user' => 'test', 15 | 'pwd' => 'password', 16 | 'db' => 'php7cookbook' 17 | ]; 18 | 19 | try { 20 | $dsn = sprintf('mysql:charset=UTF8;host=%s;dbname=%s', $params['host'], $params['db']); 21 | $opts = [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]; 22 | $pdo = new PDO($dsn, $params['user'], $params['pwd'], $opts); 23 | $sql = 'SELECT id,name,level FROM customer ORDER BY id LIMIT 20'; 24 | $stmt = $pdo->query($sql, PDO::FETCH_CLASS, 'Customer'); 25 | printf('%4s | %20s | %5s' . PHP_EOL, 'ID', 'NAME', 'LEVEL'); 26 | printf('%4s | %20s | %5s' . PHP_EOL, '----', str_repeat('-', 20), '-----'); 27 | while ($row = $stmt->fetch(PDO::FETCH_CLASS)) { 28 | printf('%4d | %20s | %5s' . PHP_EOL, $row->id, $row->name, $row->level); 29 | } 30 | } catch (PDOException $e) { 31 | echo $e->getMessage(); 32 | } catch (Throwable $e) { 33 | echo $e->getMessage(); 34 | } 35 | -------------------------------------------------------------------------------- /source/chapter05/chap_05_pdo_mysql_fetch_class_fetch_object.php: -------------------------------------------------------------------------------- 1 | 'localhost', 14 | 'user' => 'test', 15 | 'pwd' => 'password', 16 | 'db' => 'php7cookbook' 17 | ]; 18 | 19 | try { 20 | $dsn = sprintf('mysql:charset=UTF8;host=%s;dbname=%s', $params['host'], $params['db']); 21 | $opts = [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]; 22 | $pdo = new PDO($dsn, $params['user'], $params['pwd'], $opts); 23 | $sql = 'SELECT id,name,level FROM customer ORDER BY id LIMIT 20'; 24 | $stmt = $pdo->query($sql); 25 | printf('%4s | %20s | %5s' . PHP_EOL, 'ID', 'NAME', 'LEVEL'); 26 | printf('%4s | %20s | %5s' . PHP_EOL, '----', str_repeat('-', 20), '-----'); 27 | while ($row = $stmt->fetchObject('Customer')) { 28 | printf('%4d | %20s | %5s' . PHP_EOL, $row->id, $row->name, $row->level); 29 | } 30 | } catch (PDOException $e) { 31 | echo $e->getMessage(); 32 | } catch (Throwable $e) { 33 | echo $e->getMessage(); 34 | } 35 | -------------------------------------------------------------------------------- /source/chapter05/chap_05_pdo_mysql_fetch_obj.php: -------------------------------------------------------------------------------- 1 | 'localhost', 6 | 'user' => 'test', 7 | 'pwd' => 'password', 8 | 'db' => 'php7cookbook' 9 | ]; 10 | 11 | try { 12 | $dsn = sprintf('mysql:host=%s;dbname=%s', $params['host'], $params['db']); 13 | $opts = [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]; 14 | $pdo = new PDO($dsn, $params['user'], $params['pwd'], $opts); 15 | $stmt = $pdo->query('SELECT * FROM customer ORDER BY id LIMIT 20'); 16 | printf('%4s | %20s | %5s' . PHP_EOL, 'ID', 'NAME', 'LEVEL'); 17 | printf('%4s | %20s | %5s' . PHP_EOL, '----', str_repeat('-', 20), '-----'); 18 | // NOTE: uses stdClass 19 | while ($row = $stmt->fetch(PDO::FETCH_OBJ)) { 20 | printf('%4d | %20s | %5s' . PHP_EOL, $row->id, $row->name, $row->level); 21 | } 22 | } catch (PDOException $e) { 23 | echo $e->getMessage(); 24 | } catch (Throwable $e) { 25 | echo $e->getMessage(); 26 | } 27 | -------------------------------------------------------------------------------- /source/chapter05/chap_05_pdo_prepared_named_placeholders.php: -------------------------------------------------------------------------------- 1 | 'localhost', 6 | 'user' => 'test', 7 | 'pwd' => 'password', 8 | 'db' => 'php7cookbook' 9 | ]; 10 | 11 | try { 12 | $dsn = sprintf('mysql:charset=UTF8;host=%s;dbname=%s', $params['host'], $params['db']); 13 | $opts = [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]; 14 | $pdo = new PDO($dsn, $params['user'], $params['pwd'], $opts); 15 | $sql = 'SELECT * FROM customer ' 16 | . 'WHERE balance < :val AND level = :level ' 17 | . 'ORDER BY id LIMIT 20'; 18 | $stmt = $pdo->prepare($sql); 19 | $stmt->execute(['val' => 100, 'level' => 'BEG']); 20 | printf('%4s | %20s | %5s | %7s' . PHP_EOL, 'ID', 'NAME', 'LEVEL', 'BALANCE'); 21 | printf('%4s | %20s | %5s | %7s' . PHP_EOL, '----', str_repeat('-', 20), '-----', '-------'); 22 | while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { 23 | printf('%4d | %20s | %5s | %7.2f' . PHP_EOL, 24 | $row['id'], $row['name'], $row['level'], $row['balance']); 25 | } 26 | } catch (PDOException $e) { 27 | echo $e->getMessage(); 28 | } catch (Throwable $e) { 29 | echo $e->getMessage(); 30 | } 31 | -------------------------------------------------------------------------------- /source/chapter05/test.php: -------------------------------------------------------------------------------- 1 | 'localhost', 6 | 'user' => 'test', 7 | 'pwd' => 'password', 8 | 'db' => 'php7cookbook' 9 | ]; 10 | 11 | try { 12 | $dsn = sprintf('mysql:charset=UTF8;host=%s;dbname=%s', $params['host'], $params['db']); 13 | $opts = [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]; 14 | $pdo = new PDO($dsn, $params['user'], $params['pwd'], $opts); 15 | $stmt = $pdo->query('THIS SQL STATEMENT WILL NOT WORK'); 16 | printf('%4s | %20s | %5s | %7s' . PHP_EOL, 'ID', 'NAME', 'LEVEL', 'BALANCE'); 17 | printf('%4s | %20s | %5s | %7s' . PHP_EOL, '----', str_repeat('-', 20), '-----', '-------'); 18 | while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { 19 | printf('%4d | %20s | %5s | %7.2f' . PHP_EOL, 20 | $row['id'], $row['name'], $row['level'], $row['balance']); 21 | } 22 | } catch (PDOException $e) { 23 | error_log($e->getMessage()); 24 | } catch (Throwable $e) { 25 | error_log($e->getMessage()); 26 | } 27 | -------------------------------------------------------------------------------- /source/chapter06/chap_06_menu_builder_1.php: -------------------------------------------------------------------------------- 1 | 5 | Dropdown 6 | 10 | 11 | */ 12 | 13 | $factory = new Application\Web\Menu\Factory($menu, $bootstrap); 14 | $factory->setToggleTag('%s'); 15 | ?> 16 | 17 | 18 | PHP 7 Cookbook 19 | 20 | 21 | header(); ?> 22 | 23 | 24 | 25 |
26 |

Simple Bootstrap Menu

27 |
28 | render(); ?> 29 |
30 |
31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /source/chapter06/chap_06_menu_builder_2.php: -------------------------------------------------------------------------------- 1 | 5 |
  • Home
  • 6 |
  • Profile
  • 7 | 17 | 18 | */ 19 | 20 | $factory = new Application\Web\Menu\Factory($menu, $bootstrap); 21 | $factory->setDropdownWrapper('