/    Sign up×
Community /Pin to ProfileBookmark

I tried looking up in the help manuals for object and all I could find is some examples that just throw errors.

I ended up having to by trial and error make my owm and I ended up with this.

[code=php]class Object
{
function Object()
{
$this->property1 = 7;
$this->property2 = ‘localhost’;
$this->property3 = ”;
}
}
$newObject = new Object;
$newObject->Object();[/code]

Does a quicker method of making objects exist, like in javascript all you need is

[CODE]myObj = new Object; [/CODE]

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@NogDogAug 06.2006 — You cannot instantiate an object from a non-existent class; so, yes, you have to do the class definition or there is nothing to instantiate. However, you do not need the last line of your example, as the method with the same name as the class will be automatically executed upon instantiation (unless running PHP5 [i]and[/i] you also have a __construct() method defined, in which case it will be the class's constructor).
Copy linkTweet thisAlerts:
@netbuddyauthorAug 07.2006 — The original example showed something like this...
[CODE]class objectname{
var someproperty;
var anotherproperty;
var andanother;
function setmyproperty(){
$this->someproperty = 'thisvalue-1';
$this->anotherproperty = 'thisvalue-2';
$this->andanother = 'thisvalue-3';
}
}
$theobject = new objectname;
$theobject->setmyproperty();
[/CODE]


so what your saying is all I need is

[CODE]class theobjectiwant{}
$myobject = new theobjectiwant;[/CODE]


The reason I ask is because javascript has a 'class' command but you are not dependent upon it to make new objects.
Copy linkTweet thisAlerts:
@NogDogAug 07.2006 — Yes, in PHP you must define the class before you instantiate it.
×

Success!

Help @netbuddy spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.9,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...