dink

Need a little help from you pros.

I have a little function that does it's thing and does it well.  I'd like to tuck that function in a class and use it from there.  I'm stumped by my inability to pass an array as a variable to my function.

Here's the function: page_indexer() within repager.

php

 
<?

php

 

$a = array(
'dink' => 'dork',
'perk' => 'king',
'nop' => 'god',
'vs'  => 'bishop',
)

$url="ping_not.txt";

$lines=file($url);

$not_ping_list = " ";
foreach ($lines as $line_num => $line) {
    $not_ping_list = $not_ping_list.$line." ";
}

function page_indexer( $page, $case_sensitive=false ) {
global $not_ping_list;
global $a;

code that does tricks here;
}
?>


I would like to remove the array from the function and place it in a file.  That way I can change the key=>value business by changing files.  When I remove the array from the top of the repager.

php

  script and shove it into indexi.

php

  I get nothing in return.

Here's my script that calls the function: indexer.

php

 
<?

php

 
require_once "repager.

php

 ";

//Try this
//$file = "indexi.

php

 ";
//$fh = fopen($file, 'r');
//$a = fread($fh, filesize($file));
//fclose($fh);
//Didn't work

//Try again
//$a = file_get_contents("indexi.

php

 ");
//No workie slick

//This works if the array is left on the repager.

php

 
$input = file_get_contents("pages.txt");

print page_indexer($input);
?>


Knots in my knickers and twisted pantyhose. 

How can I get this to work?  Suggested reading? 

vsloathe

eeeeeeeeeeeek

Declare them as globals outside your function, buddy.

vsloathe


<?

php

 

global $a;
global $not_ping_list;

$a = array(
'dink' => 'dork',
'perk' => 'king',
'nop' => 'god',
'vs'  => 'bishop',
)

$url="ping_not.txt";

$lines=file($url);

$not_ping_list = " ";
foreach ($lines as $line_num => $line) {
    $not_ping_list = $not_ping_list.$line." ";
}

function page_indexer( $page, $case_sensitive=false ) {
code that does tricks here *and uses $a*!;
}
?>

perkiset

Passing arrays is a little weird, particularly in

PHP

 4 - it is cleaner but still not perfect in

PHP

 5.

You need to pass a *reference* to the array, not the array itself - if you pass the array itself you are passing a copy, not the real deal.


<?

php

 
$myarr = array(1,2,3,4,5);

myFunc($myArr)

function myFunc(&$theArr)
{
print_r($theArr);
}
?>


Note the ampersand in the declaration of the function - that's what does the reference bit. Objects used to need to be passed by reference, but in

PHP

  5 the processor is smart enough to know you meant the living object, not a copy of it. It is not, however, a problem ti put &$myObject as a parameter for readability.

If you are returning a reference to an array, then declare that as well in the prototype -

function &myFunc($aParam)
{
return array(1,2,3,4,5);
}

This is way better than using globals... pingback if this is incomprehensible.

/p

vsloathe

quote author=perkiset link=topic=819.msg5607#msg5607 date=1204924468

This is way better than using globals...


/agree

DangerMouse

Interesting stuff on passing references theres Perk, never really made use of the 'feature' having largely worked with

PHP

 5 objects from the outset. I'm not entirely certain where it's useful though? Allowing a variable to be manipulated within a function, and having that variable permently effected outside of that function strikes me as a little dangerous? Doesnt it kind of defeat the purpose of function scope?

DM

perkiset

Perhaps DM, but then again, depends on what you want to do.

Consider a scraper: what if you have a multi-dimension array that has all outbound links, a concatenation of the content, spots for the title, keywords etc etc... XML would be handy here as an example - a tiered construct of data that is not necessarily best kept "flat" ie., arr[0], arr[1], arr[2], but in fact more like:

$page['url'] = 'http://mysite.com/index.html';
$page['rawbuff'] = '';
$page['title'] = 'blah blah';
$page['keywords'][0] = "viagara"
$page['keywords'][1] = "levitra"
$page['content'] = 'blah blah blah';
$page['links']['internal'][0] = '...';
$page['links']['external'][0] = '...';

then you might want to write a method that looked like this:

<?

php

 

$page = array();
$page['url'] = 'http://mysite.com';
collectPage($page);
collectInternalLinks($page);
collectExternalLinks($page);
collectContent($page);

?>


This is a rather simplistic example, but I think it articulates why you may wish to be able to pass an array, rather than just globalifying it. Another thought here is if you use collectInternalLinks on other things BESIDES just a global variable - you want to pass in a an array that has a 'rawbuff' entry, then the collectInternalLinks can work on it whether it was really collected as a page or not and additionally, the collectInternalLinks function needs have no knowledge of how the rest of your script operates.

It makes for a more cleanly divided and reusable toolset. Wow, I've had enough coffee for one day...  Applause

DangerMouse

Applause cool example thanks! Funnily enough its very similar to something i've been building recently. I can see how this would be particularly useful where multiple classes have to work on an item and thus you couldnt use a property of an object to hold the array (from the example).

Interesting stuff, you

learn

  something new everyday  Applause - cheers!

DM

perkiset

nw mate - have a great Friday

dink

Damn.  I thought I had munged my post enough that it wouldn't be apparent what I was doing.  Then I noticed you didn't include Cialis in the example, so you prolly just read my thoughts.  (note to selfApplauserder more tinfoil boyo)

First, thanks for the speedy, and unexpectedly polite, replies.

Next up...I think I muddled things up too much &| gave too much info.

The global stuff was intended to perform as Perk explained in the last post.  However, my tactics have been modified.  I no longer care if the array is local, global, international, or intragalactic.  The data pairs in the array(s) will very likely never be needed by any other operation.

That said, what I'm having trouble with is pointing a variable at files, which contain the data pairs, in a manner that the function code can access them via the variable.

I have tried the file() thingies with no success.  Hell, I might as well leave it alone.  It only takes a few mo to copy and paste the function code.  Classes are for sissies.  Applause

perkiset

I'm half crocked, so this'll prolly be a stoopid answer.

First, LOL @ tinfoil. Don't fret, lucky shot. Maybe. Applause

I am confused about how you want "variables from files..." - let me pop out a couple quickies and see if I'm getting warmer, cause I haven't got a frappin' clue what you are asking.

You can obviously have a file that looks like this:
<?
$myVar = 'this and that';
?>

... and then include it and it becomes code. You could even create that file on the fly and then include it if you were feeling particularly tricky.

You can eval('$myVar = "this and that"; ') and it will be evaluated as code. So you could also load a buffer from a file_get_contents('/adir/afile.

php

 ') and then eval it. I am not certain if the <? and ?> are required in that case. In fact, I don't think they are.

You can create an array an store it like this:
$myArr = array(1,2,3,4,5);
$arrStr = serialize($myArr);
file_put_contents('/adir/afile.txt', $arrStr);

... which will put a serialized version of the array into afile.txt, then:

$myArr = unserialize(file_get_contents('/adir/myfile.txt'));

and you'll have the array back in black.

Cripes I need another drink. Good luck, I'll be in and out, ping if you need something.

/p


thedarkness

quote author=dink link=topic=819.msg5624#msg5624 date=1204942625

I have tried the file() thingies with no success.  Hell, I might as well leave it alone.  I


you mean like:

$handle = fopen("/home/dink/file.txt", "r");

then pass $handle around?

quote author=dink link=topic=819.msg5624#msg5624 date=1204942625

Classes are for sissies.  Applause


Them's fightin' words.

Cheers,
td

dink

quote author=thedarkness link=topic=819.msg5627#msg5627 date=1204960542

you mean like:

$handle = fopen("/home/dink/file.txt", "r");

then pass $handle around?


zigzackly.

quote

Them's fightin' words.


Oooops.  Does this mean what I think it means?

@ Perk
quote

I am confused about....


Entirely understandable.  After all, you have a heathen hillbilly hick trying to communicate with the Elbert Ienstine of

php

 .  We need an interpreter between us.  Now where is basil when I need him. Applause

Let me restate the original situation.
I have this neato little function that is controlled by the input of three types of data.  The possibilities for (ab)use with this system are only limited by my evil imagination.  Applause

One set of data is, or will be, mostly static and therefore easily incorporated in the script.

The second set of data is a series of text files.  A library, if you will, of some 150 files.  This data is transmitted to the function as a variable argument to the function.

The third set of data is a series of files containing data pairs in the form of arrays.  Another library (fifteen files right now.).  Since it is arbitrary, and not part of the argument, I was having difficulty passing the arrays to the function by use of a variable.

After your <>drunken spiel excellent suggestions, I got it to work.  Applause

None of the eval() attempts produced anything except growling and gripes from

Apache

 .  A simple include <>inside the function did work.  That didn't solve the problem of using a variable to pass the array to the function, so was not a desirable solution.

After many shots, I finally hit the target.  In my indexer.

php

  page, which calls the function, I placed this snippet:
$a = include "pages.

php

 ";

pages.

php

  has the opening and closing tags and this statement:
$a = array {
~ ~
}
return $a;

Since I have the include() on the control page, indexer.

php

 , it is outside the function.  Therefore I use the Global notation within the function to get it to work.

**sidebar**
According to my interpretation of the manual, a Global declaration that is inside the function makes it a defacto local, not accessible outside the namespace, but what the hell do I know(?).
**/sidebar**

Man oh man.  That's a lot of content for the spyderiskies to grab up.  I'd tell you I'm sorry but you wouldn't believe me anyway. Applause

Thanks to all.  It works a treat.  All your commissions are belong to me.  Applause

((Google domination is only mere steps away.  Applause  ))





Perkiset's Place Home   Politics @ Perkiset's