PHP framework Questions with Answers

10 practice questions on PHP framework (PHP) with answers and step-by-step solutions. Try 10 samples below, then practise all 10 free in the AptiNest app.

Sample questions

Question 1Medium

The goal of each controller in Symfony is to return a

  1. Response object
  2. Request Object
  3. PHP Object
  4. None

Answer: A. Response object

Explanation

A controller is a PHP function you create that takes information from the HTTP request and constructs and returns an HTTP response (as a Symfony2 Response object). The response could be an HTML page, an XML document, a serialized JSON array, an image, a redirect, a 404 error or anything else you can dream up.

Step-by-step solution

  1. Controller returns HTTP response as Response object.
  2. Response object handles different types of output.
  3. Correct answer is Response object.
  4. Final answer: A (Response object).

Why the other options are wrong

  • B. A Request object is what the controller receives, not what it returns.
  • C. A generic PHP object is not what a Symfony controller must return.
  • D. None is wrong because a controller returns a Response object.

Tip: Remember, controllers always return a Response object.

Question 2Medium

The filename 'Zend/Db/Table.php' must map to the class name___?

  1. Zend table
  2. Zend_Db_Table

Answer: B. Zend_Db_Table

Explanation

The Zend_Db_Table class is an object-oriented interface to database tables. It provides methods for many common operations on tables. The base class is extensible, so you can add custom logic.

Step-by-step solution

  1. In Zend Framework, each slash in the file path becomes an underscore in the class name.
  2. So Zend/Db/Table.php maps to the class Zend_Db_Table.
  3. Zend db table, option B, is that name with the underscores read as spaces, the correct answer.

Why the other options are wrong

  • A. Zend table drops the Db part of the path, so a segment is missing.

Tip: In Zend-style names each folder in the path becomes a word, joined by underscores.

Question 3Medium

The Request class symfony is a simple object-oriented representation of the____

  1. HTTP request message
  2. HTML request message
  3. PHP request message
  4. TCP request message

Answer: A. HTTP request message

Explanation

The Request class symfony is a simple object-oriented representation of the HTTP request message.

Step-by-step solution

  1. The Request class in Symfony handles incoming web requests.
  2. HTTP is the protocol used for transmitting data on the web.
  3. Symfony's Request class represents the HTTP request message as an object.
  4. This allows developers to access and manipulate request data easily.
  5. Therefore, the correct answer is HTTP request message.

Why the other options are wrong

  • B. HTML is a markup language for pages; the Request class represents an HTTP message.
  • C. PHP is the language itself; the Request class represents an HTTP request, not a PHP request message.
  • D. TCP is the transport layer; the Request class models the HTTP request message.

Tip: Remember: Symfony's Request class deals with HTTP requests, not HTML or PHP.

Question 4Medium

Which of the following are php frameworks?

  1. Zend framework
  2. Symphony framework
  3. Cake php framework
  4. All of these.

Answer: D. All of these.

Explanation

A framework is designed to provide a structure for common elements (database interaction, presentation layer, application logic) so you spend less time writing up database interface code or presentation-layer interfaces and more time writing the application itself.

Step-by-step solution

  1. The question asks which of the listed names are PHP frameworks.
  2. Zend framework is a PHP framework.
  3. Symfony (written Symphony in the question) is a PHP framework.
  4. CakePHP is a PHP framework, so all three are right and the correct answer is option D, All of these.

Why the other options are wrong

  • A. Zend is a real PHP framework, but it is not the full answer on its own; the question needs all of the named options.
  • B. Symfony is a real PHP framework, but on its own it is not the full answer; All of these is the right choice.
  • C. CakePHP is a real PHP framework, but on its own it is not the full answer; All of these is the right choice.

Tip: When All of these is an option, check every listed item; it is right only if every item is correct.

Question 5Medium

Validators used with zend form should implement which class?

  1. Zend_form_ValidatorAbstract
  2. Zend_Validate_Abstract
  3. Zend_validate_Interface
  4. Zend form validate interface

Answer: C. Zend_validate_Interface

Explanation

Zend_Validate_Interface defines two methods, isValid() and getMessages(), that may be implemented by user classes in order to create custom validation objects. An object that implements Zend_Validate_Interface interface may be added to a validator chain with Zend_Validate::addValidator().

Step-by-step solution

  1. The question asks which interface a custom Zend form validator must implement.
  2. Zend_Validate_Interface defines the isValid() and getMessages() methods.
  3. Zend_Validate_Abstract is a base class, not the interface that validators implement.
  4. The correct answer is option C, Zend_validate_Interface.

Why the other options are wrong

  • A. Zend_form_ValidatorAbstract is not the interface a validator implements.
  • B. Zend_Validate_Abstract is a base class, not the interface named in the question.
  • D. This is not a class or interface name in Zend.

Tip: Interfaces list the methods a class must have; pick the interface, not the base class.

Question 6Medium

In Symfony, the routing configuration can be written in

  1. YAML
  2. PHP
  3. XML
  4. All of the above

Answer: D. All of the above

Explanation

In symphony ,the routing configuration can be written in YAML, in PHP and in Xml.

Step-by-step solution

  1. The question asks which formats Symfony accepts for routing configuration.
  2. The explanation says Symfony routing can be written in YAML, in PHP and in XML.
  3. All three are listed, so option D, All of the above, is the answer.

Why the other options are wrong

  • A. YAML is valid, but PHP and XML are valid too.
  • B. PHP is valid, but YAML and XML are valid too.
  • C. XML is valid, but YAML and PHP are valid too.

Tip: When every listed choice is true, All of the above is the answer.

Question 7Medium

ZendAcl supports __ inheritance among resource objects.

  1. Cyclic
  2. Multiple
  3. No
  4. Single

Answer: D. Single

Explanation

ZendAcl supports single inheritance among resource objects, meaning a resource can only have one parent resource.

Step-by-step solution

  1. The question asks what kind of inheritance Zend ACL supports among resource objects.
  2. The explanation says a resource can have only one parent resource.
  3. One parent per resource is single inheritance, which is option D.

Why the other options are wrong

  • A. Cyclic would mean inheritance loops back on itself, which is not described.
  • B. Multiple would mean a resource can have many parents, but the explanation says one.
  • C. No would mean no inheritance at all, but inheritance is supported.

Tip: Single means one parent; multiple means many parents.

Question 8Medium

In CakePHP, user can define their own routes in the

  1. app/Config/routes.php
  2. app/Config/temp.php
  3. app/Config/url.php
  4. app/Config/connect.php

Answer: A. app/Config/routes.php

Explanation

CakePHP defines custom routes in app/Config/routes.php. The other file names are not CakePHP's routing file.

Step-by-step solution

  1. CakePHP routes are defined in app/Config/routes.php.
  2. The $routes array in this file lets you set custom routing rules.
  3. Wildcards or regular expressions can be used to define these routes.
  4. Other files like temp.php, url.php, and connect.php do not handle routing.
  5. So the correct route definition file is app/Config/routes.php.

Why the other options are wrong

  • B. temp.php is not the CakePHP routes file; user routes are defined in routes.php.
  • C. url.php is not the CakePHP routes file; user routes are defined in routes.php.
  • D. connect.php is not the routes file; user routes are defined in routes.php.

Tip: Remember: routes.php is where you set up URL routing in CakePHP.

Question 9Medium

Which method of zend locale will check if given string is locale?

  1. IsLocale
  2. areLocale

Answer: A. IsLocale

Explanation

IsLocale checks if the given string is a valid locale identifier.

Step-by-step solution

  1. IsLocale method checks if a string is a valid locale identifier.
  2. Valid locales are like 'en_US' or 'fr_FR', so IsLocale confirms this.
  3. Other methods like areLocale do not perform this check directly.
  4. Therefore, the correct answer is IsLocale.
  5. Final Answer: A (IsLocale).

Why the other options are wrong

  • B. areLocale is a different name from the method asked for; the checking method here is IsLocale.

Tip: Remember, IsLocale is used to validate locale strings directly.

Question 10Medium

By default, the routing configuration file in a Symfony2 application is located at

  1. app/config/routing.yml
  2. app/yaml/routing.yml
  3. app/config_route/routing.yml
  4. app/config/routing.config

Answer: A. app/config/routing.yml

Explanation

In a Symfony 2 project, the default routing file is app/config/routing.yml. The other paths are not the default.

Step-by-step solution

  1. Symfony2 uses YAML format for routing configuration.
  2. The default location for routing.yml is app/config/routing.yml.
  3. Other options have incorrect folder names or file extensions.
  4. Option A matches the standard Symfony2 directory structure.
  5. Final answer: app/config/routing.yml.

Why the other options are wrong

  • B. app/yaml is not the default folder; the routing file sits in app/config.
  • C. app/config_route is not the default folder; the routing file sits in app/config.
  • D. routing.config has the wrong extension; the default file is routing.yml.

Tip: Remember, Symfony2 uses YAML files in app/config for most configurations.

Practise all 10 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all PHP topics · All topics