Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

9/1/2021 Creating an Object (Programming PHP)

home |
O'Reilly's CD bookshelfs |
FreeBSD | Linux | Cisco |
Cisco Exam
 

6.2. Creating an Object


Ads by
It's
much easier to create objects and use them than it is to define
object classes, so before we discuss how to define classes,
let's look at creating objects. To create an object
of a given class, use the
new
keyword:
Send feedba
$object = new Class;
Why this ad? 
Assuming that a Person class has been defined,
here's how to create a Person
object:

$rasmus = new Person;

Do not quote the class name, or you'll get a


compilation error:

$rasmus = new 'Person'; // does not work

Ads by Some classes permit you to pass arguments to the


new call. The class's
documentation should say whether it accepts
arguments. If it does,
you'll create objects like this:

Send feedback $object = new Person('Fred', 35);

Akudemy
The class name does not have to be hardcoded into your program. You
can supply the class name through a variable:
Why this ad? 
-
$class = 'Person';
$object = new $class;
Interview
// is equivalent to
$object = new Person;
Coaching
Specifying a class that doesn't exist causes a
runtime error.
Looking for a

JavaScript job? Variables containing object


references are just normal variables—they can be used in the
same ways as other variables. Of
Joining Top tech
particular note is that variable
variables work with objects, as shown here:
can be easy, with
$account = new Account;
focused
$object = 'account'
preparations.
${$object}->init(50000, 1.10); // same as $account->init
We'll guide you!

github.com
6. Objects 6.3. Accessing Properties and
Methods

OPEN

Copyright © 2003 O'Reilly & Associates. All rights reserved.

Ads by
Send feedback Why this ad? 
AWS Jobs
Hiring talents with 4+ years of experience to
Implementation Specialist

Hexaware Technologies Ltd


Ads by
Send feedback Why this ad? 

https://docstore.mik.ua/orelly/webprog/php/ch06_02.htm 1/2
9/1/2021 Creating an Object (Programming PHP)

Ads by

Send feedback
Akudemy

Why this ad? 


-

Interview

Coaching
Looking for a

JavaScript job?
Joining Top tech

can be easy, with

focused

preparations.

We'll guide you!

github.com

OPEN

Ads by
Send feedback Why this ad? 

https://docstore.mik.ua/orelly/webprog/php/ch06_02.htm 2/2

You might also like