Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 07-12-2005, 11:50 AM   #1 (permalink)
metazai
Regular Contributor
 
Join Date: Apr 2004
Location: Orange County, CA
Posts: 122
metazai is on a distinguished road
Arrow, Arrow, who's got the arrow?

Ok, I've made several PHP-enabled sites, and done what I think is some pretty snazzy programming. I know, of course, that compared to professionals, most of it is simple and clumsy, but I'd like to think that I have learned something about PHP. I've taught myself, however, which leaves some startling gaps in my knowledge. I'm about to undertake a monumental task in PHP, and in reading samples of similar applications to what I want to build, I'm running into a question that is so completely pathetic that I could only post it in a forum on a site with the word "Newbie" in the title:

What's with the arrow? I'm having trouble figuring out the exact use of something like this:

$this->inv_table."some text"

It's the little -> that gets me.

Can someone explain what it's for, so I can use it, or at least understand it?

Use small words. I'm obviously a little brain-difisshigntaskdjf4*$*%(&
metazai is offline   Reply With Quote
Old 07-12-2005, 12:03 PM   #2 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,471
sde is on a distinguished road
first of all, no need for the 'pathetic question' setup =) .. it's a great question.

-> in php is how you access functions or variables of an object. in your example, this would be the object.

you only use the keyword this when you are accessing properties of a class that you are currently coding in.

i wrote this article a while back. it's just a little introduction to object oriented (OO) programming with php.

Lots of things have changed with OO programming and PHP with PHP5. keep in mind that the link above was written for php4, so it may not be proper PHP5 as far as the style of a class goes.

to answer your question exactly, you are just concatinating aclass varaible with "some_text". the dot in PHP just glues strings together.

it would be the same thing as this:
PHP Code:
<?
$inv_table 
"<table border=1><tr><td>";
$end_table "</td></tr></table>";

echo 
$inv_table."some text".$end_table;
?>
Other languages such as Java and Javascript are a little different to PHP in this case. They use a + to concatinate ( where php uses a dot ) .. also they use a dot to access a class property ( where php uses -> )

if it was a class, it might look something like this:
PHP Code:
<?
class MyClass(){
  
$this->inv_table "<table border=1><tr><td>";
  
$this->end_table "</td></tr></table>";

  function 
printTextInTable($some_text){
    echo 
$this->inv_table $some_text $end_table;
  }

  
// this is an example of calling a function
  // within the class with this->
  
function printWelcome(){
    
$this->printTextinTable("Welcome");
  }
}

// now i can use my class to make tables with text
$mc = new MyClass();

// now call the function inside the class.. except this time
// we will be using $mc instead of $this since we are outside the class
$mc->printTextInTable("this will print inside an html table");
?>
probably not the best examples, but feel free to ask more questions if you don't understand.
__________________
Mike
sde is offline   Reply With Quote
Old 07-12-2005, 12:23 PM   #3 (permalink)
metazai
Regular Contributor
 
Join Date: Apr 2004
Location: Orange County, CA
Posts: 122
metazai is on a distinguished road
Cool. I think I . . . almost get it. I don't feel so bad not know what it was now that I know it was OOP, which I'd understood was a method of coding PHP but not one I've used, not coming from a java/C/coder background. Be forewarned you will be seeing further questions on this board from me in the coming weeks.

One question now from the brief but hightly informative article you wrote . . . in the function "article", you have
Code:
  function article($id) 
  { 
    $result=mysql_query("select * from articles where id='" . $id . "'"); 
     
    // when defining variables that belong to this class, while inside the class, 
    // use 'this->' before the variable name. 
    $this->id = $id; 
    $this->headline = mysql_result($result,0,"headline");
I'm assuming that $this->headline would get the result from the matching id query, so you could spell that line out like so:

"$this->headline is a class variable that is equal to the data marked "headline" which was pulled from the proper id tag in the query"

But what's the ",0," in the middle for?

Also, unless you don't think it belongs here, using the same example how would you pull an array? Or is that what the "0" indicates?
metazai is offline   Reply With Quote
Old 07-12-2005, 12:36 PM   #4 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,471
sde is on a distinguished road
that was written a long time ago .. i should update it as my coding style has changed a lot since then .. the 0 indicates the first record of the rows that are returned in the mysql query when using the mysql_result function.

it wold be the same as saying
PHP Code:
<?
if($row mysql_fetch_assoc($result)){
  
$this->headline $row['headline'];
}
?>
__________________
Mike
sde is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

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 On
Pingbacks are On
Refbacks are On



All times are GMT -8. The time now is 04:02 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting