RE: [PHP] simple class & constructor
| From: | Tommy Pham | Date: | Tue, 19 Oct 2010 23:53:56 +0000 |
| Subject: | RE: [PHP] simple class & constructor | ||
| References: | 1 2 3 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-308863@lists.php.net to get a copy of this message | ||
> -----Original Message-----
> From: David McGlone [mailto:david@dmcentral.net]
> Sent: Tuesday, October 19, 2010 4:32 PM
> To: php-general@lists.php.net
> Subject: Re: [PHP] simple class & constructor
>
> On Tue, 2010-10-19 at 17:15 -0400, Paul M Foster wrote:
> > On Tue, Oct 19, 2010 at 04:12:51PM -0400, David McGlone wrote:
> <snip>
> > You're trying to "instantiate the class". And the way you're doing it
> > here is correct. When you do this, $test becomes an object of this
> > class. If you had another function ("member") within the class called
> > "myfunction()", you could run it this way (after you instantiate the
> > class):
> >
> > $test->myfunction();
> >
> > >
> > > Basically I want to learn how I can (if it's possible with this
> > > simple
> > > code) is display the output on a different page.
> > >
> > > I tried putting the line: $test=new simpleConstructer(); on the
> > > index page and including the page the class is on, but it causes the
> > > index page to go blank.
> >
> > You've likely got an error you're not seeing. Fix this first. If the
> > file your class is in is syntactically correct, and you do
> >
> > include "simpleConstructerFile.php";
> >
> > in your index.php file, it should flawlessly include the code. Then,
> > in your index.php, you do this:
> >
> > $test = new simpleConstructer;
> >
> > you should see the contents of the echo statement appear on the page.
> > So you're on the right track. You just need to find the error first.
>
>
> Ah ha! Thank you! Your mention of an error, was spot on. notice below I
> misspelled the class name but got the Object name correct.
>
> Also at first I had the setup like this because it wasn't working and I thought
> I was doing it wrong: (this also added to my confusion)
>
> myclass.php
>
> class simpleConstructer {
>
> function __construct() {
> echo "running the constructor";
> }
> }
>
> index.php
> require_once 'myclass.php';
> $test = new simpleConstructor();
>
> But once I fixed the error I put it all back in myclass.php like so:
>
> myclass.php
>
> class simpleConstructer {
>
> function __construct() {
> echo "running the constructor";
> }
> }
> $test = new simpleConstructor();
>
>
> Now I am wondering what you meant when you said:
> >>>If you had another function ("member") within the class called
> >>>"myfunction()", you could run it this way (after you instantiate the
> >>> class):
>
> >>>$test->myfunction();"
>
> If you don't mind my asking, how would you take the above example and
> change it to what you describe above?
>
class simpleConstructer {
function __construct() {
echo "running the constructor";
}
function myFunction() {
echo 'this is another function/method within the class simpleConstructor';
}
}
$test = new simpleConstructor();
$test->myfunction();
Regards,
Tommy
>
>
> --
> Blessings
> David M.
>
>
>