br3nn4n Posted August 3, 2009 Share Posted August 3, 2009 Here's my code: <?php function query($query) { $q = mysql_query($query) or die("Error, please try later"); $q = mysql_fetch_array($q); return $q; } ?> And I'm getting: Parse error: syntax error, unexpected T_VAR in ... As I said I'm really new to OOP (in general) and classes in PHP, so I'm just missing something simple. Any help Quote Link to comment https://forums.phpfreaks.com/topic/168723-me-new_to_oop-cant-declare-new-variable-in-a-method/ Share on other sites More sharing options...
abazoskib Posted August 4, 2009 Share Posted August 4, 2009 that error might not necessarily have anything to do with that function. post more code. Quote Link to comment https://forums.phpfreaks.com/topic/168723-me-new_to_oop-cant-declare-new-variable-in-a-method/#findComment-890181 Share on other sites More sharing options...
ToonMariner Posted August 4, 2009 Share Posted August 4, 2009 We do, as abaz says, need to see more code to put this in context Quote Link to comment https://forums.phpfreaks.com/topic/168723-me-new_to_oop-cant-declare-new-variable-in-a-method/#findComment-890225 Share on other sites More sharing options...
br3nn4n Posted August 4, 2009 Author Share Posted August 4, 2009 Ah geez, pardon me, I didn't even notice I posted an earlier version of that segment. Sorry about that, I'll get the new code and or old error when I can get to a computer, shouldn't be long. Quote Link to comment https://forums.phpfreaks.com/topic/168723-me-new_to_oop-cant-declare-new-variable-in-a-method/#findComment-890285 Share on other sites More sharing options...
br3nn4n Posted August 4, 2009 Author Share Posted August 4, 2009 Here's the right code: <?php class admin { public $page; function admin($page) { $this -> page = $page; $this->db_connect(); } function db_connect() { mysql_connect('......stripped....'); mysql_select_db("wri"); mysql_query("SET NAMES 'utf8'"); return $this; } function query($query) { var $q; $q = mysql_query($query) or die("Error, please try later"); $q = mysql_fetch_array($q); return $q; } function title() { // some crazy querying shit here... $title = ucfirst($this->page); return $title; } function page() { $this->query("SELECT * FROM pages WHERE page='".$this->page."' AND administrative='1'"); $this->content = print_r($this->q); return $this; } function content() { return $this->content; } } //site class ?> Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/168723-me-new_to_oop-cant-declare-new-variable-in-a-method/#findComment-890299 Share on other sites More sharing options...
RichardRotterdam Posted August 4, 2009 Share Posted August 4, 2009 What version of php are you running? perhaps php 4? I dont get this error "Parse error: syntax error, unexpected T_VAR in ..." I do see something wrong in the following function query($query) { var $q; $q = mysql_query($query) or die("Error, please try later"); $q = mysql_fetch_array($q); return $q; } The var infront of the $q shouldnt be inside a function remove var here Quote Link to comment https://forums.phpfreaks.com/topic/168723-me-new_to_oop-cant-declare-new-variable-in-a-method/#findComment-890391 Share on other sites More sharing options...
aschk Posted August 4, 2009 Share Posted August 4, 2009 Here's a PHP5 valid version of your code with PHPDoc comments: <?php /** * Admin Class * @author br3nn4n */ class admin { /** * Name of the page. * @var string */ private $_page; /** * String containing the page content. * @var string */ private $_content; /** * Constructor * @param string $page */ function __construct($page) { $this->setPageName($page) ->initDB(); } /** * Initialise the database connection. */ protected function initDB() { mysql_connect('......stripped....'); mysql_select_db("wri"); mysql_query("SET NAMES 'utf8'"); return $this; } /** * Set the internal page name. * @param string $page * @return admin - Allows chaining of methods. */ public function setPageName($page){ $this->_page = $page; return $this; } /** * Execute SQL query and return 1st row as numeric indexed array. * @param string $query * @return array */ public function query($query) { $q = mysql_query($query) or die("Error, please try later"); $q = mysql_fetch_array($q); return $q; } /** * Get the title based on page name. * @return string */ public function title() { // some crazy querying shit here... $title = ucfirst($this->getPageName()); return $title; } /** * Retrieve the name of the page. * @return string */ protected function getPageName(){ return $this->_page; } /** * Calling page() loads the content from the DB internally. * @return admin - Allows method chaining. */ public function function page() { $page = $this->query("SELECT * FROM pages WHERE page='{$this->getPageName()}' AND administrative='1'"); // This is a guess but i'm loading the 1st part of the return query array as the content. $this->_content = $page[0]; return $this; } /** * Get the "page" content. * @return string */ public function content() { if(!$this->_content){ $this->page(); } return $this->_content; } } /** * @example */ $admin = new admin(); print $admin->content(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/168723-me-new_to_oop-cant-declare-new-variable-in-a-method/#findComment-890556 Share on other sites More sharing options...
br3nn4n Posted August 5, 2009 Author Share Posted August 5, 2009 Alright...! haha, like I admitted I'm still struggling to understand OOP so your example (as simple as it is) has me sort of confused. To clarify: I know about chaining (and returning $this..), that makes sense. But what about the underscores before variables? Not talking about the double underscore to declare the constructor, that's something I get also. But why not just use $page, instead of $_page? Sorry if I'm being a bug :] Quote Link to comment https://forums.phpfreaks.com/topic/168723-me-new_to_oop-cant-declare-new-variable-in-a-method/#findComment-891377 Share on other sites More sharing options...
RichardRotterdam Posted August 5, 2009 Share Posted August 5, 2009 But what about the underscores before variables? Not talking about the double underscore to declare the constructor, that's something I get also. But why not just use $page, instead of $_page? Anyone correct me if i am wrong. I think it's a style preference to make the difference more clear between public variables and protected + private vars. You don't have to use the underscores it will work fine without it. Quote Link to comment https://forums.phpfreaks.com/topic/168723-me-new_to_oop-cant-declare-new-variable-in-a-method/#findComment-891383 Share on other sites More sharing options...
br3nn4n Posted August 5, 2009 Author Share Posted August 5, 2009 But what about the underscores before variables? Not talking about the double underscore to declare the constructor, that's something I get also. But why not just use $page, instead of $_page? Anyone correct me if i am wrong. I think it's a style preference to make the difference more clear between public variables and protected + private vars. You don't have to use the underscores it will work fine without it. Alright, that was sort of what I thought (just a formality / style thing). @aschk: And I plugged in your code and got this: Warning: Missing argument 1 for admin::__construct(), called in ...../admin_class.php on line 106 and defined in ...../admin_class.php on line 25 Notice: Undefined variable: page in ...../admin_class.php on line 26 Error, please try later And I'm sure I'm instantiating the object with the variable, so what could be wrong? I fixed your minor double "function" already, that was a separate error but it's gone now Thank you much! Quote Link to comment https://forums.phpfreaks.com/topic/168723-me-new_to_oop-cant-declare-new-variable-in-a-method/#findComment-891397 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.