p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

beginning_php thread: classes


Message #1 by "Randy Bartels" <bunchofpixels@a...> on Sat, 30 Jun 2001 21:24:30
on page 296 of Beginning PHP4 it has these lines of code:



$BelgianSleuth=new detective("Hercule Poirot");

$VictorianSleuth=new detective("Sherlock Holmes");

echo $BelgianSleuth->name //echos Hercule Poirot

echo $Victorian Sleuth->name //echos Sherlock Holmes





How do i make a class where i can define properties in the same line that 

i create an instance of a class like in the examples above?
Message #2 by "7stud" <7stud@e...> on Sun, 1 Jul 2001 19:12:00
Hi,



Where in this line, which creates an instance of "detective" : 



$BelgianSleuth=new detective("Hercule Poirot");



are there any properties defined?



Message #3 by "Randy Bartels" <bunchofpixels@a...> on Sun, 1 Jul 2001 20:17:35
the detective class uses the string passed to it to define the name 

property.  i wanted to know how something like that could be done.
Message #4 by "7stud" <7stud@e...> on Mon, 2 Jul 2001 06:45:32
> the detective class uses the string passed to it to define the name 

> property.  i wanted to know how something like that could be done.



It absolutely does not use the string to define the name property.  

The "name" member already has to be defined, AND something called a 

constructor already has to be defined, and THEN when you instantiate a 

member of the detective class, the string that is used as a parameter to 

the constructor is used to INITIALIZE the "name" member.



If you read the whole chapter on objects, the book will explain how to do 

that. 
Message #5 by BunchOfPixels@a... on Mon, 2 Jul 2001 08:39:59 EDT
ok yea the constructor uses the string and stores it in the name property






  Return to Index