What is the basic syntax to declare a class in PHP?
Use the <code>class</code> keyword followed by the class name and curly braces. Example:<br><pre>class MyClass {<br> // properties and methods<br>}</pre>
Click to reveal answer
beginner
What keyword is used to create a class in PHP?
The keyword <code>class</code> is used to declare a class in PHP.
Click to reveal answer
beginner
Can a PHP class contain properties and methods? Give a simple example.
Yes, a PHP class can have properties (variables) and methods (functions). Example:<br><pre>class Car {<br> public $color;<br> function drive() {<br> echo "Driving";<br> }<br>}</pre>
Click to reveal answer
beginner
Is it necessary to end a PHP class declaration with a semicolon?
No, PHP class declarations do not end with a semicolon after the closing brace.
Click to reveal answer
beginner
How do you name a class in PHP? Are there any rules?
Class names should start with a letter or underscore, followed by letters, numbers, or underscores. They are case-insensitive but it's common to use PascalCase. Example: <code>class MyClassName {}</code>
Click to reveal answer
Which keyword is used to declare a class in PHP?
Aclass
Bfunction
Cobject
Ddeclare
✗ Incorrect
The class keyword is used to declare a class in PHP.
What symbol is used to enclose the body of a PHP class?
AAngle brackets <>
BSquare brackets []
CParentheses ()
DCurly braces {}
✗ Incorrect
Curly braces {} are used to enclose the class body.
Which of the following is a valid PHP class name?
A_UserProfile
B3DModel
Cuser-profile
Dclass
✗ Incorrect
Class names can start with a letter or underscore, but not a number or hyphen. _UserProfile is valid.
Do you need a semicolon after the closing brace of a PHP class declaration?
AYes, always
BOnly if the class has properties
CNo, never
DOnly if the class is empty
✗ Incorrect
No semicolon is needed after the closing brace of a class declaration.
Which of these can be inside a PHP class?
AOnly methods
BProperties and methods
COnly properties
DNeither properties nor methods
✗ Incorrect
A PHP class can contain both properties (variables) and methods (functions).
Explain how to declare a simple class in PHP including properties and methods.
Think about the structure: keyword, name, then body with variables and functions.
You got /5 concepts.
What are the rules for naming a class in PHP?
Consider what characters are allowed and common naming styles.
You got /5 concepts.
Practice
(1/5)
1. What is the correct way to start declaring a class named Car in PHP?
easy
A. class Car { }
B. function Car() { }
C. Car class { }
D. declare class Car { }
Solution
Step 1: Understand PHP class declaration syntax
In PHP, classes are declared using the keyword class followed by the class name and curly braces.
Step 2: Match the syntax to the options
class Car { } uses class Car { }, which is the correct syntax. Other options use incorrect keywords or order.
Final Answer:
class Car { } -> Option A
Quick Check:
PHP class declaration = class ClassName { } [OK]
Hint: Look for 'class' keyword followed by name and braces [OK]
Common Mistakes:
Using 'function' instead of 'class'
Placing 'class' after the name
Adding extra keywords like 'declare'
2. Which of the following is the correct syntax to declare an empty class named Person in PHP?
easy
A. class Person() { }
B. class Person[] { }
C. class Person {};
D. class Person { }
Solution
Step 1: Recall PHP class declaration syntax
PHP classes are declared with the keyword class, the class name, and curly braces without parentheses or brackets.
Step 2: Check each option
class Person { } uses class Person { }, which is correct. class Person() { } wrongly uses parentheses, B uses brackets, and D adds a semicolon after braces which is unnecessary.
Final Answer:
class Person { } -> Option D
Quick Check:
Empty class = class Name { } [OK]
Hint: No parentheses or brackets after class name [OK]
Common Mistakes:
Adding parentheses after class name
Using square brackets after class name
Adding unnecessary semicolon after class braces
3. What will be the output of this PHP code?
class Dog {
public $name = "Buddy";
}
$dog = new Dog();
echo $dog->name;
medium
A. Buddy
B. Dog
C. Error: Undefined property
D. null
Solution
Step 1: Understand the class and property
The class Dog has a public property $name set to "Buddy".
Step 2: Create an object and access property
Creating $dog = new Dog(); makes an object. Accessing $dog->name returns "Buddy".
Final Answer:
Buddy -> Option A
Quick Check:
Access public property = property value [OK]
Hint: Public properties can be accessed directly via object [OK]
Common Mistakes:
Expecting class name instead of property value
Forgetting to create an object before access
Assuming property is private and inaccessible
4. Identify the error in this PHP class declaration:
class Animal
public $type;
}
medium
A. Class name should be lowercase
B. Missing opening brace { after class name
C. Property must be static
D. Missing semicolon after class name
Solution
Step 1: Check class declaration syntax
PHP classes require an opening brace { immediately after the class name.
Step 2: Analyze the given code
The code has no opening brace after class Animal, causing a syntax error.
Final Answer:
Missing opening brace { after class name -> Option B
Quick Check:
Class declaration needs { after name [OK]
Hint: Always put { right after class name [OK]
Common Mistakes:
Omitting opening brace {
Thinking semicolon needed after class name
Confusing property declaration rules
5. You want to declare a PHP class Book with a property $title initialized to "PHP Basics" and a method getTitle() that returns the title. Which is the correct class declaration?
hard
A. class Book {
public $title;
public function getTitle() {
return $title;
}
}
B. class Book {
public $title = "PHP Basics";
function getTitle() {
return title;
}
}
C. class Book {
public $title = "PHP Basics";
public function getTitle() {
return $this->title;
}
}
D. class Book {
public $title = "PHP Basics";
public function getTitle() {
return $Book->title;
}
}
Solution
Step 1: Declare property with initial value
Property $title is declared public and initialized to "PHP Basics" correctly in class Book {
public $title = "PHP Basics";
public function getTitle() {
return $this->title;
}
}.
Step 2: Define method returning property
Method getTitle() must return $this->title to access the object's property. class Book {
public $title = "PHP Basics";
public function getTitle() {
return $this->title;
}
} does this correctly.
Step 3: Check other options for errors
class Book {
public $title = "PHP Basics";
function getTitle() {
return title;
}
} returns title without $this->, class Book {
public $title;
public function getTitle() {
return $title;
}
} returns $title which is undefined in method scope, class Book {
public $title = "PHP Basics";
public function getTitle() {
return $Book->title;
}
} uses $Book->title which is invalid inside class.
Final Answer:
class Book {
public $title = "PHP Basics";
public function getTitle() {
return $this->title;
}
} -> Option C
Quick Check:
Use $this->property inside methods [OK]
Hint: Use $this->property to access inside methods [OK]
Common Mistakes:
Returning property without $this-> inside method
Using variable names without $this->
Trying to access property with class name variable
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.