Connecting Tech Pros Worldwide Forums | Help | Site Map

PHP Classes

Johnny
Guest
 
Posts: n/a
#1: Aug 14 '08
Hi everybody,

I am trying to set up two classes in the following manner:

class adodb_connection
{
public function Execute($sSql)
{
//do something with $sSql here...
$this->Execute=new adodb_recordset;
$this->Execute->Fields=5;
}
}

class adodb_recordset
{
public $Fields;
}


When i am trying to get the Fields variable of adodb_recordset at the
following manner, i get a "Notice: Trying to get property of non-object in
..." error.

=>>>>

$connMain = new adodb_connection;
//$rstMain = new adodb_recordset;

$rstMain = $connMain->Execute('sql');
echo $rstMain->Fields;


1) Can someone tell me why this isn't working?
2) If i change $this->Execute->Fields=5; in function Execute to
$this->Execute->Fieldszzzzzzzzzzzzzzzzzzzz=5; i get no error...! Shouldn't
php come with an error stating that the variable Fieldszzzzzzzzzzzzzzzzzzzz
isn't found??


I hope that someone has the solution for me.

Thanks in advance..!

Johnny.




=?iso-8859-1?Q?=C1lvaro?= G. Vicario
Guest
 
Posts: n/a
#2: Aug 14 '08

re: PHP Classes


*** Johnny escribió/wrote (Thu, 14 Aug 2008 19:25:46 +0200):
Quote:
class adodb_connection
{
public function Execute($sSql)
{
//do something with $sSql here...
$this->Execute=new adodb_recordset;
$this->Execute->Fields=5;
}
}
>
class adodb_recordset
{
public $Fields;
}
>
>
When i am trying to get the Fields variable of adodb_recordset at the
following manner, i get a "Notice: Trying to get property of non-object in
.." error.
>
=>>>>
>
$connMain = new adodb_connection;
//$rstMain = new adodb_recordset;
>
$rstMain = $connMain->Execute('sql');
echo $rstMain->Fields;
Execute() doesn't return any value so $rstMain is NULL, not an object. I
guess you mean:

echo $connMain->Execute->Fields;


Also, class adodb_connection have both a property and a method (undeclared)
with the same name. Although PHP allows that, it looks pretty confusing.

Quote:
2) If i change $this->Execute->Fields=5; in function Execute to
$this->Execute->Fieldszzzzzzzzzzzzzzzzzzzz=5; i get no error...! Shouldn't
php come with an error stating that the variable Fieldszzzzzzzzzzzzzzzzzzzz
isn't found??
I do get the very same error than above. In any case, in PHP it's optional
to declare class attributes.



--
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://bits.demogracia.com
-- Mi web de humor en cubitos: http://www.demogracia.com
--
Johnny
Guest
 
Posts: n/a
#3: Aug 14 '08

re: PHP Classes


Hi Álvaro,

Thank you for your quick response.
Quote:
Quote:
>Execute() doesn't return any value so $rstMain is NULL, not an object.
After adding the following line to method Execute, no more errors occured:

return $this->Execute;

Quote:
Quote:
>Also, class adodb_connection have both a property and a method
>(undeclared)
>with the same name. Although PHP allows that, it looks pretty confusing.
Do you mean when putting $this->Execute=new adodb_recordset; in method
Execute, this isn't referencing the method Execute, but a new variable named
Execute??

Thanks,

Johnny



"Álvaro G. Vicario" <webmasterNOSPAMTHANKS@demogracia.comschreef in
bericht news:1oicobora4ats.84shtw1toxy7$.dlg@40tude.net...
Quote:
*** Johnny escribió/wrote (Thu, 14 Aug 2008 19:25:46 +0200):
Quote:
>class adodb_connection
>{
> public function Execute($sSql)
> {
> //do something with $sSql here...
> $this->Execute=new adodb_recordset;
> $this->Execute->Fields=5;
> }
>}
>>
>class adodb_recordset
>{
>public $Fields;
> }
>>
>>
>When i am trying to get the Fields variable of adodb_recordset at the
>following manner, i get a "Notice: Trying to get property of non-object
>in
>.." error.
>>
>=>>>>
>>
>$connMain = new adodb_connection;
>//$rstMain = new adodb_recordset;
>>
>$rstMain = $connMain->Execute('sql');
>echo $rstMain->Fields;
>
Execute() doesn't return any value so $rstMain is NULL, not an object. I
guess you mean:
>
echo $connMain->Execute->Fields;
>
>
Also, class adodb_connection have both a property and a method
(undeclared)
with the same name. Although PHP allows that, it looks pretty confusing.
>
>
Quote:
>2) If i change $this->Execute->Fields=5; in function Execute to
>$this->Execute->Fieldszzzzzzzzzzzzzzzzzzzz=5; i get no error...!
>Shouldn't
>php come with an error stating that the variable
>Fieldszzzzzzzzzzzzzzzzzzzz
>isn't found??
>
I do get the very same error than above. In any case, in PHP it's optional
to declare class attributes.
>
>
>
--
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://bits.demogracia.com
-- Mi web de humor en cubitos: http://www.demogracia.com
--

=?iso-8859-1?Q?=C1lvaro?= G. Vicario
Guest
 
Posts: n/a
#4: Aug 14 '08

re: PHP Classes


*** Johnny escribió/wrote (Thu, 14 Aug 2008 21:54:11 +0200):
Quote:
Quote:
Quote:
>>Also, class adodb_connection have both a property and a method
>>(undeclared)
>>with the same name. Although PHP allows that, it looks pretty confusing.
>
Do you mean when putting $this->Execute=new adodb_recordset; in method
Execute, this isn't referencing the method Execute, but a new variable named
Execute??
Yes. In that sense PHP is different from JavaScript.

However, it wouldn't make much sense to have a function that commits
suicide the first time it's called :-?


--
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://bits.demogracia.com
-- Mi web de humor en cubitos: http://www.demogracia.com
--
Closed Thread