Current location: Hot Scripts Forums » Programming Languages » PHP » PHP - class/ basic question


PHP - class/ basic question

Reply
  #1 (permalink)  
Old 01-18-08, 01:03 PM
SurfboYne SurfboYne is offline
Newbie Coder
 
Join Date: Jan 2008
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
PHP - class/ basic question

Okay this is my first time using a class, so you know.

I have a function in my class that needs to save all pubdates from an XML file in an array that I want to be able to read outside the class (anywhere in the PHP script, like global).

Reading and parsing the XML is fine. I just can't save the value (pubdate) in an array without losing it. When the next pubdates comes, the previous one is lost.

So how can I make an array with values that I can use outisde the class ?
Reply With Quote
  #2 (permalink)  
Old 01-18-08, 01:51 PM
Jay6390's Avatar
Jay6390 Jay6390 is offline
Code Master
 
Join Date: Apr 2007
Location: United Kingdom
Posts: 1,330
Thanks: 0
Thanked 0 Times in 0 Posts
Some code would help identify the problem and solution a bit clearer
__________________
Useful Tutorials
[ PHP Video-1-2-3 ] [ MySQL 1-2-3 ]
For any php function reference type

www.php.net/FunctionName
Reply With Quote
  #3 (permalink)  
Old 01-18-08, 05:05 PM
SurfboYne SurfboYne is offline
Newbie Coder
 
Join Date: Jan 2008
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
The code is really too big to paste here. It's just a function($value) that has to save all values in an array.

I'm not even sure if this is possible.
Reply With Quote
  #4 (permalink)  
Old 01-18-08, 05:22 PM
Jay6390's Avatar
Jay6390 Jay6390 is offline
Code Master
 
Join Date: Apr 2007
Location: United Kingdom
Posts: 1,330
Thanks: 0
Thanked 0 Times in 0 Posts
So you are wanting to pass an array to $class->function($array) and have it saved so that you can retrieve the values? or do you mean something else?
__________________
Useful Tutorials
[ PHP Video-1-2-3 ] [ MySQL 1-2-3 ]
For any php function reference type

www.php.net/FunctionName
Reply With Quote
  #5 (permalink)  
Old 01-18-08, 07:08 PM
phpdoctor's Avatar
phpdoctor phpdoctor is offline
Code Guru
 
Join Date: Feb 2007
Location: New Zealand
Posts: 767
Thanks: 4
Thanked 2 Times in 2 Posts
By lossing the data, do you mean when the page is refreashed? (you cant do this)

Take a look at this example class: (Untested)
PHP Code:

<?php


class TestClass
{
    public 
$test_var = array() ;

    public function 
test_method($param)
    {
        [
B]$this->test_var[/B] = $param ;
    }
}

$class = new TestClass ;
$test_array = array('Hello''World') ;
$class->test_method($test_array) ;

print_r($class->test_var) ; // [0] = Hello, [1] = World

?>
The code in bold is what i think you need to learn and get your head around.
To get a propertie inside the class you use $this->. after the -> you put the variable name (without the $)

Also Functions inside a class are called Methods...

Good Luck, feel free to ask questions on this
Lex
__________________
01010000 01001000 01010000
Reply With Quote
  #6 (permalink)  
Old 01-19-08, 02:08 AM
Jay6390's Avatar
Jay6390 Jay6390 is offline
Code Master
 
Join Date: Apr 2007
Location: United Kingdom
Posts: 1,330
Thanks: 0
Thanked 0 Times in 0 Posts
Yeah, that will work doc, but it is good practice to not have the property available for manipulation directly within a class, and to have it available only through the methods, otherwise it may as well not even have the method as he will be able to just set the var directly ie
PHP Code:

<?php 

class TestClass 

    public 
$test_var = array(); 

    public function 
test_method($param
    { 
        [
b]$this->test_var[/b] = $param 
    } 


$class = new TestClass 
$test_array = array('Hello''World') ;

#This line is redundant
#$class->test_method($test_array) ;
#and instead just set it with
$this->test_var=$test_array;

print_r($class->test_var) ; // [0] = Hello, [1] = World 

?>
That being said, if you are wanting to manipulate the data each time, you are best off using a method, setting the var to private, and having a get and set method for the value to guarantee the variable is properly stored each time, and not be altered accidentally outside of the class. therefore you would need something like this

PHP Code:

<?php 

class TestClass 

    private 
$test_var = array() ; 

    public function 
method_get() 
    { 
        return 
$this->test_var
    }

    public function 
method_set($param
    {
        
#Perform actions on var here, including data manipulation and error checking
        
$this->test_var $param 
    } 


$class = new TestClass 
$test_array = array('Hello''World') ; 
$class->method_set($test_array) ; 

print_r($class->method_get()) ; // [0] = Hello, [1] = World 

?>
Something I would like to know myself is is it better to set the var as an array directly, or in the __construct of the class itself, if it even makes any difference

Jay
__________________
Useful Tutorials
[ PHP Video-1-2-3 ] [ MySQL 1-2-3 ]
For any php function reference type

www.php.net/FunctionName
Reply With Quote
  #7 (permalink)  
Old 01-19-08, 05:48 AM
SurfboYne SurfboYne is offline
Newbie Coder
 
Join Date: Jan 2008
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Okay thanks, I found it!
Reply With Quote
  #8 (permalink)  
Old 01-20-08, 01:56 PM
phpdoctor's Avatar
phpdoctor phpdoctor is offline
Code Guru
 
Join Date: Feb 2007
Location: New Zealand
Posts: 767
Thanks: 4
Thanked 2 Times in 2 Posts
Ah good point Jay, thanks for that!
__________________
01010000 01001000 01010000
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP efficiency/running time question Agum PHP 4 05-03-07 06:00 AM
WebGrub.co.uk PHP Class Repository tobydeh Script Requests 6 06-09-06 06:47 PM
php class fix/rewrite borzoid Job Offers & Assistance 1 06-07-06 11:05 PM
Basic help on PHP noviceforever PHP 2 10-20-04 08:11 PM
general question about php and html timfoster PHP 3 05-17-04 12:29 PM


All times are GMT -5. The time now is 07:33 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.