I have a class.php
file, in which I include a square from another file I
class.php:
Then, under /includes/user.php
the class is defined as $ user
.
And there is a function called /includes/user.php
getSocialUser ($ username);
:
function getSocialUser ($ username) {global $ DBH; $ Stmt = $ dbh- & gt; Prepare ("Choose user WHERE user name =: user name"); $ Stmt- & gt; Baidam Parm (': Username', $ username); $ Stmt- & gt; Executed (); $ Udata = $ stmt-> Fetch (); If (calculating ($ $) == 0) returned incorrectly; Return $ $; }
( $ dbh
holds the database value. I tested it, and it is related to the database)
However, I want to be able to use that function inside the class.php
file:
I am now doing this:
class .php:
Include ($ _ server ['DOCUMENT_ROOT']. "/includes/user.php"); $ User = New User; Class feed {function random function ($ username) {global $ user; $ Username = "test"; $ Viewer = $ user- & gt; GetSocialUser ($ username); Resonate $ viewuser ['avatar']; }}
The above code does not work. I get this error:
Fatal error: a member function getSocialUser () in line 6 / Call non-object at path / class.php
What am I doing wrong?
Getting Started Using Global, Bad Practices Instead, add database connection as a parameter , So change your function to
function getSocialUser ($ dbh, $ username) {$ stmt = $ dbh- & gt; Ready ("SELECT * users from WHERE user name =: username"); $ Stmt- & gt; Baidam Parm (': Username', $ username); $ Stmt- & gt; Executed (); $ Udata = $ stmt-> Fetch (); If (calculating ($ $) == 0) returned incorrectly; Return $ $; }
Next, start database connection and user class in building your class,
class feed {personal $ dbh; Private $ user; Function __ conversion () {$ this- & gt; Dbh = new PDO ('your data here'); $ This- & gt; User = new user (); } Function random function ($ username) {$ username = "test"; $ Viewer = $ this- & gt; User- & gt; GetSocialUser ($ this-> DBH, $ username); Resonate $ viewuser ['avatar']; }}
Comments
Post a Comment