Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Classes Var, whats wrong?
Old 03-06-2008, 09:57 AM Classes Var, whats wrong?
dansgalaxy's Avatar
Defies a Status

Posts: 6,521
Name: Dan
Location: Swindon
Trades: 0
Okay, im having a go at building a class and am trying to make some vars which would be accessed like $page->title

so i have like this:

PHP Code:
class page {
 var 
$pgtitle;
 
 var 
$title         setting('main_title''style_template');
 var 
$meta_robots   setting('meta_robots''style_template');
 var 
$meta_keywords setting('meta_keywords''style_template');
 var 
$meta_desc     setting('meta_desc''style_template');
 var 
$footer_txt1   setting('footer_txt1''style_template'); 
but im getting this error:
Code:
Parse error: syntax error, unexpected '(', expecting ',' or ';' in /home/ukcalm/public_html/includes/class/page.class.inc.php on line 7


Whats wrong?
__________________
Discounted Web Hosting With XDnet!
>> Get 25% of hosting~ Promo: Webmaster-talk <<

Please login or register to view this content. Registration is FREE
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
 
Register now for full access!
Old 03-06-2008, 10:29 AM Re: Classes Var, whats wrong?
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
Trades: 0
I don't think its legal to pass a function when declaring vars. You can assign the values to the vars in the class constructor

class page {
var $pgtitle;
var $title;
var $meta_robots;
var $meta_keywords;
var $meta_desc;
var $footer_txt1;

function page() {
$this->title = setting('main_title', 'style_template');
. . .
__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is offline
Reply With Quote
View Public Profile
 
Old 03-06-2008, 11:11 AM Re: Classes Var, whats wrong?
dansgalaxy's Avatar
Defies a Status

Posts: 6,521
Name: Dan
Location: Swindon
Trades: 0
oky so if i do that would i then be able to use them within the template like $page->title correct?

Dan
__________________
Discounted Web Hosting With XDnet!
>> Get 25% of hosting~ Promo: Webmaster-talk <<

Please login or register to view this content. Registration is FREE
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Old 03-06-2008, 11:44 AM Re: Classes Var, whats wrong?
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
Trades: 0
If the object is assigned to the var $page outside of the class, you can use it like that.
__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is offline
Reply With Quote
View Public Profile
 
Old 03-06-2008, 11:52 AM Re: Classes Var, whats wrong?
dansgalaxy's Avatar
Defies a Status

Posts: 6,521
Name: Dan
Location: Swindon
Trades: 0
okay this is what i have so far in my class:
PHP Code:

class page 
{
 var 
$pgtitle;
 var 
$title;
 var 
$meta_robots;
 var 
$meta_author;
 var 
$meta_keywords;
 var 
$meta_desc;
 var 
$footer_txt1;
 var 
$footer_txt2;
 
 function 
vars()
 {
 
$this->title         setting('main_title''style_template');
 
$this->meta_robots   setting('meta_robots''style_template');
 
$this->meta_author   setting('meta_author''style_template');
 
$this->meta_keywords setting('meta_keywords''style_template');
 
$this->meta_desc     setting('meta_desc''style_template');
 
$this->footer_txt1   setting('footer_txt1''style_template');
 
$this->footer_txt2   setting('footer_txt2''style_template');
 }
 
 
 function 
last_update()
 {
  
$last_update setting('last_update''site_info');
   
$last_update strtotime($last_update);
   return 
date('d/m/Y H:i'$last_update); //Date time format can become a setting.
 

now the last_update func works,

but when i try to use $page->title it now showing anything (same for all of them)

what am i doing wrong?
__________________
Discounted Web Hosting With XDnet!
>> Get 25% of hosting~ Promo: Webmaster-talk <<

Please login or register to view this content. Registration is FREE
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Old 03-06-2008, 11:57 AM Re: Classes Var, whats wrong?
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
Dan, 2 things...

First:
Use php5 declaration, if you can.
You can use the keywords "public", "private" and "protected".
The php4 declaration, like you used, default to "public" in php5.

Public means that the property/function is accessible from the outside of the object.
Private means the property/function is accessible only in the object.
Protected means that the property/function is accessible only from the object or from an object who's extended the parent object.

PHP Code:
class clTest{
  public 
$title;
  private 
$content;

  public function 
__construct($title,$content){
    
$this->title=$title;
    
$this->content=$content;
  }

and in PHP4:
PHP Code:
class clTest{
  var 
$title;
  var 
$content;

  function 
clTest($title,$content){
    
$this->title=$title;
    
$this->content=$content;
  }

the __construct function is the constructor in PHP5. In PHP4, it's a function that have the same name as the class. in this case, it would be a function clTest($title,$content);

Second:
When the constructor have parameters, you can get them when you create an object of your class
PHP Code:
var $obj=new clTest('This is the title','This is the content'); 
__________________
Only a biker knows why a dog sticks his head out the window.

Last edited by tripy; 03-06-2008 at 11:58 AM..
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 03-06-2008, 12:12 PM Re: Classes Var, whats wrong?
dansgalaxy's Avatar
Defies a Status

Posts: 6,521
Name: Dan
Location: Swindon
Trades: 0
Okay sorry but you lost me after the public/private thing.

Think of my like a gold fish. (at the moment im still banging my head at the glass because i can see a reflection of food which is behind me...)

okay i didnt know that var was for php, will change now.

but whats the other thing im confused now :s

i dont see why its not working, im using a mysql db class i have as a basis :s

Dan
__________________
Discounted Web Hosting With XDnet!
>> Get 25% of hosting~ Promo: Webmaster-talk <<

Please login or register to view this content. Registration is FREE
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Old 03-06-2008, 12:59 PM Re: Classes Var, whats wrong?
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
Trades: 0
Dan, is the function setting() declared as a function outside the scope of the class? If not, you will need to build it, or if the setting() is declared inside your class, it will nedd to be accessed by $this->setting()
__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is offline
Reply With Quote
View Public Profile
 
Old 03-06-2008, 01:22 PM Re: Classes Var, whats wrong?
dansgalaxy's Avatar
Defies a Status

Posts: 6,521
Name: Dan
Location: Swindon
Trades: 0
the setting() func is declared outside the class, and i know that it works because it works within the last_update function, which at present is the only part which works...

So as far as i know the setting() is not the prob. just syntax and a moron (me)
__________________
Discounted Web Hosting With XDnet!
>> Get 25% of hosting~ Promo: Webmaster-talk <<

Please login or register to view this content. Registration is FREE
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Reply     « Reply to Classes Var, whats wrong?
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.67425 seconds with 12 queries