Re: simple OO question
| From: | Andrew Halliday | Date: | Mon, 05 Mar 2001 04:17:34 +0000 |
| Subject: | Re: simple OO question | ||
| References: | 1 2 3 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-42573@lists.php.net to get a copy of this message | ||
No, you cant do this as you coded it, however minor alterations will allow
this.
Last time i checked, you couldnt initialise variables in object scope ...
you can only declare them. This makes sense, because thats what the
constructor is for.
Altered code for what you want to do is as follows:
class myclass{
var $fields;
var $columns;
function myclass { // constructor
$this->fields = mysql_list_fields("database1", "table1", $link);
$this->columns = mysql_num_fields($fields);
}
// all code must be declared in a function !! - This isnt Java Script :)
function doSomething() {
for ($i = 0; $i < $this->columns; $i++) {
var mysql_field_name($this->fields, $i);
}
}//function doSomething()
}//class
$myobj = new myclass(); // creates object and runs constructor (see above)
$myobj->doSomething(); // do whatever you want the object to do
Andrew H
> class myclass{
>
> $fields = mysql_list_fields("database1", "table1", $link);
> $columns = mysql_num_fields($fields);
>
> for ($i = 0; $i < $columns; $i++) {
> var mysql_field_name($fields, $i);
> }
>
> }//class
>
> now.. can i do this?
> is there a performnance issue here?
> can i put this in the constructor?
>
> john
>
>
>
>
> Andrew Halliday wrote:
>
> > yes you can do all of this
> >
> > but in no OO language so far have i seen the ability to access a
variable
> > inside a method...you would have to do this:
> >
> > class myclass
> > {
> > var $myvar;
> > function setmyvar($newmyvar)
> > {
> > $this->myvar = $newmyvar;
> > }
> > }
> >
> > Then you could do this:
> >
> > $myclassObj = new myclass();
> > $myclassObj->setmyvar(10);
> > echo $myclassObj->myvar;
> >
> > This would print '10'...
> >
> > Cut and paste that exact program in and it should be a working
demonstration
> > ...
> >
> > You should read the php manual more ...
> > see
> > www.php.net
> >
> > AndrewH
> > ----- Original Message -----
> > From: "John LYC" <john@edlabs.com>
> > To: "PHP List" <php-general@lists.php.net>
> > Sent: Monday, March 05, 2001 1:36 PM
> > Subject: [PHP] simple OO question
> >
> > > can i do this..?
> > >
> > > //declaring class
> > >
> > > class myclass{
> > >
> > > //declaring properties
> > > ...
> > > ....
> > >
> > > //declaring methods..
> > > function mymethod(){
> > >
> > > //can i declared variable in method?
> > > var $myvar;
> > > ...
> > > //do something
> > >
> > > }//end of mymethod
> > >
> > > ///////////////////
> > >
> > > if yes, do i access myvar like this.
> > > $item = new myclass;
> > > print $item->mymethod()->myvar;
> > >
> > >
> > > thanks
> > > john
> > >
> > >
> > >
> > >
> > >
> > > --
> > > PHP General Mailing List (http://www.php.net/)
> > > To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net
> > > For additional commands, e-mail: php-general-help@lists.php.net
> > > To contact the list administrators, e-mail:
php-list-admin@lists.php.net
> > >
> >
> > --
> > PHP General Mailing List (http://www.php.net/)
> > To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net
> > For additional commands, e-mail: php-general-help@lists.php.net
> > To contact the list administrators, e-mail: php-list-admin@lists.php.net
>