RE: simple class & constructor

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Tue, 2010-10-19 at 16:53 -0700, Tommy Pham wrote:
> > -----Original Message-----
> > From: David McGlone [mailto:david@xxxxxxxxxxxxx]
> > Sent: Tuesday, October 19, 2010 4:32 PM
> > To: php-general@xxxxxxxxxxxxx
> > Subject: Re:  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();

Thank you Tommy.

Now it all comes together and I believe I understand now.

Does the code immediately after the __construct automatically run, but
when adding more methods to the class, they need to be called with the
$name->Object_name? Is my thinking correct?

-- 
Blessings
David M.


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux