PHP - Two Questions: 1) Calling Parent Function 2) Static Function
Question 1) Is the only and proper way to call a parent function "parent::function()"? Are there other/better ways from within a child function?
Question 2) What are the deciding factors for when to make a function or attribute static? How do you make that decision? Assuming 5.3... Thanks. Similar TutorialsI thought I was a beginner PHPer but now I'm not even sure I'm that. I have this class here which starts with: Code: [Select] private static $FormatType = ""; but then further down: Code: [Select] public function SetFormatType($NewFormatTypeId) { $this->FormatType = $NewFormatTypeId; } public function GetFormatType() { return $this->FormatType; } Do you suppose that the original developer just had a singleton or something, then latter on he found that he was making more instances and PHP tolerates this behavior? Or am I missing something? Hello all, I have some piece of code that is nested like this $variable = 'This is a global argument'; function parentFunction($variable) { function childFunction() { echo 'Argument of the parent function is '.$GLOBALS['variable']; } childFunction(); } parentFunction(5); What I want to know is - Is there a way to access a variable from the parent function without passing arguments to the child function? (Something like how classes have parent::?). I don't want to use $GLOBALS because it might cause some variable collision, and I didn't want to pass arguments because incase I decide to change the arguments in the parent function I'd have to do it in the child function aswell. From my searching around in the Internet it seems like this is not possible, but if theres a slight chance that there might be something out there, i'm willing to give it a shot . Thanks in advance I need to call usort from a class function, and I'm puzzled about how to define the comparison function. I've tried to define the comparison function in the same class, but I can't get usort to call it. I found one hint that it will work if I make the comparison function static, but I tried that, and it didn't work for me. If I define the comparison function outside the class, it won't have access to object properties that it needs to operate. The only solution I can think of is to define the comparison function outside the class and put the object properties it needs in globals. Is there a cleaner way to do this? I have the following static method: public static function query($query){ self::$num_queries++; return mysql_query($query)or die(mysql_error()); } I then call it like this: $oql = Mysql::query("SELECT first FROM members WHERE member_id = '{$this->owner}'"); $rql = mysql_fetch_assoc($oql); I then get the following error: Quote Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /incl/php/classes/Photo.php on line 13 Why am I getting this error? I have 2 php files. I am unable to get B's global variable from A's static method: A.php Code: [Select] class c_A { public static function f_A() { include_once( "B.php" ) ; print f_B() ; } } c_A::f_A( ); // only prints "B : " B.php Code: [Select] $gvs = "global variable from B" ; function f_B() { return "B : " . $GLOBALS[ "gvs" ] ; } any thoughts? thanks, Shannon can you do that with php? Code: [Select] Database2() { function test($id) { $newdb = new Database2(); $newdb->test($id); } } Hi, I have been trying to improve my PHP programming skills by following the Lynda.com Beyond the basics tutorial and they came to a section where they were building common database methods inside a database class. Since they did not have PHP 5.3 (Where late static bindings is introduced) they had to place code referring to self:: inside of each class that needs this functionality. It seems that not having late static binding was binding self to the database class and not to other classes that call it statically. I will insert some code to explain this better. Long story short, I was really interesting in getting an attributes function working and as I do not have late static bindg either, I cannot figure out how to call it from inside a class to make it function correctly. Here is some code. Below is the attribute class they wrote. Each class declared an array of attributes for each of the classes that want to call this method like protected static $db_fields = array('id', 'email', 'password', 'etc'); Code: [Select] protected function attributes() { // return an array of attribute names and their values $attributes = array(); foreach(self::$db_fields as $field) { if(property_exists($this, $field)) { $attributes[$field] = $this->$field; } } return $attributes; } They planned on moving this code to a DatabaseObject class after they get LSB but until then, have to include it in every class that wants use this. Is there any way to write this as a function, include it in a class, and call the method without late static binding.. or even place this inside a database class and call it from another class without LSB? Forgive me if I am not explaining this correctly, again, I am still learning. My version of PHP is 5.2.6. I am trying to write a more elegant solution to be able to check an object, find only the properties in an object that have values, and update those values in a database with a matching ID. I am extending a class from a generic user class here is an example of my code Code: [Select] <?php require_once('User.php'); require_once('Database.php'); class Vendor extends User { protected static $vendor_attributes = array('id', 'nothing' , 'tagline' , 'bio'); protected static $table_name = "user_vendors"; public $tagline; }//close class ?> I am using simple machines forum, and by placing a link to ssi.php in the top of my php page Code: [Select] <?php require_once('/forum/SSI.php');?>I am able to use some ssi functions. My problem is calling another function from within a function. My first function is this: Code: [Select] <?php $allowed_groups = array(1,5); $can_see = FALSE; foreach ($allowed_groups as $allowed) if (in_array($allowed, $user_info['groups'])) { $can_see = TRUE; break; } if ($can_see) { echo ' <div> this is text that i want certain groups to see </div>'; } else { //message or section to show if they are not allowed. echo 'this is text that i dont want certain groups to see'; } ?> This is my second function: Code: [Select] <?php ssi_welcome(); ?> I want to put the second function inside the first function, like so: Code: [Select] if ($can_see) { echo ' <div> <?php ssi_welcome(); ?> </div>'; this just gives me the "<?php ssi_welcome(); ?>" displayed on the actual site, it is not calling the separate function. I have tried different approaches, e.g. $function ssi_welcome; but all it does is display this as text as well.. Please can some one point me in the right direction. Thank you Hi, I got a little problem with calling a PHP function from a href. I got my site all set up, and am calling the function like this: Code: [Select] <?php echo '<a href="'.changeRecentPosts().'" class="image">' ?> This does indeed work, but it calls the function every time I refresh my page.. Which isn't the effect I was trying to achieve. I tried to change it to : Code: [Select] <?php echo '<a href="#" onClick="'.changeRecentPosts().'" class="image">' ?> But that didn't work either. It also seems it only calls it when I refresh my page, but never when I click it (I guess because PHP is handeled before HTML or something similar?). If you can think of any way (even in AJAX, or whatever other language...) feel free to, I think I can figure it all out. Thank you! I have this piece of code: Code: [Select] function newmessage() { box = new LightFace({ title: 'New message', width: 600, height: 400, content: "<form method='post' action='sendmessage.php'>To: <br><input type='text' name='to'><br><br>Subject: <input type='text' size='94' name='subject'><br><br><textarea name='message' rows='13' cols='63'></textarea><br><br><input type='submit' value='Send' name='send'><input name='from' type='hidden' value='<? echo $username; ?>'></form>", buttons: [ { title: 'Close', event: function() { this.close(); } } ] }); box.open(); } As well as this piece of code: Code: [Select] $userid = mysql_real_escape_string($_SESSION['userid']); //WALLNOT $db2 = "SELECT status FROM comments WHERE touser='$username' AND status='0'"; $db2connect = $database->query($db2); $status2 = "SELECT status FROM friends2 WHERE user2='$username' and status='0'"; $test = $database->query($status2); $statfromdb = "SELECT stat FROM wallposts WHERE person='$username' AND stat='0'"; $status = $database->query($statfromdb); $GetStatus = "SELECT byuser, status, dtime FROM commentstatus WHERE status='$Naw'and person='$username' AND byuser!='$username'"; $ConnectGetStatus = $database->query($GetStatus); $Getmsg = "SELECT status FROM messages WHERE status='0' AND touser='$username'"; $ConnectGetmsg = $database->query($Getmsg); $row = mysql_fetch_array($test); $row2 = mysql_fetch_array($ConnectGetmsg); $getrow = mysql_fetch_array($status); $getrow2 = mysql_fetch_array($db2connect); $StatusComment = mysql_fetch_array($ConnectGetStatus); //Select profile picture $GetProfilePicture = "SELECT profilepicture FROM users WHERE username='$username'"; $ConnectProfilePicture = $database->query($GetProfilePicture); $ProfilePicture = mysql_fetch_array($ConnectProfilePicture); $MyProfilePicture = $ProfilePicture['profilepicture']; That I use in 19 different pages, is there anyway I can put them all into one page, and just them through a function? It takes so much time when loading if I have it on each page? And if you have any other tips how to optimize my loading speed please write them. Thank you in advance! *Edit: Bad grammar. Hi, I was looking around the web for a image resizer script and I came across this one. http://www.scriptol.com/scripts/thumbnail-maker.php The weird thing is, it says to use this syntax to get it to work php resizer.php -j myimage.png Well.. I don't seem to understand how that makes it work, Normally I would run a php script by looking for it's main function and running it like: mainfunction(); Maybe I'm interpreting it wrong, but how is this script suppose to run? When ever I try to google this I always get it the other way round "Calling PHP from jQuery" which is now what I need. Basically I want, When the script starts, For PHP to check if an array is empty and if it's not call a jQuery function (This would be the jQuery UI Dialog box). If you can help me here or even post a link to a resource then I will kiss ya. No homo. Thanks in advance! Whenever I want to use AJAX (using jQuery) I store the PHP code in a seperate file and then send an retrieve data from that file using AJAX. However I was wondering if you can just call a PHP function. Then it would save me having to put each function in a seperate file? Thanks for any help. Code: [Select] <?php class My { public function disp() { echo "ehllo"; } } ?> <html> <head> <script type="text/javascript"> function displaymessage() { document.write(My.disp()); } </script> </head> <body> <form> <input type="button" value="Click me!" onclick="displaymessage()" /> </form> </body> </html> HEllo I wanted to know wheter php function can be call in js functions. Please Help. Hi all! First off, sorry if this question is a completely dumb one but i'm really new to PHP. I'll paste the code to start with... function getReasons ($id) { global $link; $rst = mysql_query('SELECT r.name as reason FROM entries_feedback_comments as f LEFT JOIN reasons as r ON r.id = f.reason_id WHERE f.feedback_id ='. $id, $link); while ($row = mysql_fetch_assoc($rst)) { echo $row['reason']; } } later on in the code.... inside a table structure i do this... echo '<tr class="' . ($y?'even':'odd') . '">'; echo '<td colspan="10" class="freetext">'.getReasons($row['id']).'</td>'; echo '</tr>'; echo '<tr class="' . ($y?'even':'odd') . '">'; echo '<td colspan="10" class="freetext">'.$row["freetype"].'</td>'; echo '</tr>'; (I have left put the "freetype" bit of code in to show you that it works with this row but not with the function). Basically, what I see is the result of the function outside of the table itself rather than in the table row. Is there a reason for this? Does a function have to be put in specific places in the code? Thank you for your help. Sam Hi guys, I am having a tough time executing the code inside the javascript. Everything is working fine with the Php code and I am able to see the output of variable myValue too.. But the problem is nothing is being executed inside the javascript..Not even the first statement of the javascript..When I look at the output sent by the HTML to the browser, the output is what is listed after this code which is right at the bottom of this page. Please advise. Code: [Select] <html> <head> <title>AES (Rijndael) Encryption Test in JavaScript</title> <script src="aes-enc.js" type="text/javascript" language="JavaScript"></script> <script src="aes-dec.js" type="text/javascript" language="JavaScript"></script> <script src="aes-test.js" type="text/javascript" language="JavaScript"></script> <script type="text/javascript"> function doDecryption() { document.println("Inside Javascript"); var ct, key; var theForm = document.forms[0]; blockSizeInBits=128; keySizeInBits = theForm.keySize[theForm.keySize.selectedIndex].value; if (theForm.key.value.toLowerCase().indexOf("0x") == 0) theForm.key.value = theForm.key.value.substring(2); if (theForm.ciphertext.value.toLowerCase().indexOf("0x") == 0) theForm.ciphertext.value = theForm.ciphertext.value.substring(2); if (theForm.key.value.length*4 != keySizeInBits) { alert("For a " + keySizeInBits + " bit key, the hex string needs to be " + (keySizeInBits / 4) + " hex characters long."); if (theForm.key.select) theForm.key.select(); return; } if (theForm.ciphertext.value.length*4 != blockSizeInBits) { alert("For a " + blockSizeInBits + " bit block, the hex ciphertext string needs to be " + (blockSizeInBits / 4) + " hex characters long."); if (theForm.ciphertext.select) theForm.ciphertext.select(); return; } ct = hex2s(<?php echo $myValue; ?>); document.println("Inside Javascript"); document.write(ct); // key = hex2s(theForm.key.value); // theForm.plaintext.value = byteArrayToHex(rijndaelDecrypt(ct, key, "ECB")); } </script> </head> [syntax=php] <?php mysql_connect("localhost","root",""); mysql_select_db("encryption") or die(mysql_error()); $userId = $_POST['userId']; if (($_SERVER['REQUEST_METHOD'] == 'POST') && ($_POST['key'] == "")) { $query = mysql_query("select * from employee_details where id = '$userId'"); if($row=mysql_fetch_assoc($query)) { echo '<tr>'; foreach($row as $value) echo '<td>'.$value.'</td>'; echo '</tr>'; } else { echo "No rows returned"; }} else if (($_SERVER['REQUEST_METHOD'] == 'POST') && ($_POST['key'])) { $columname = "ciphertext"; $tablename = "employee_details"; function getField($field, $tbl_name, $condition) { $result = mysql_query("SELECT $field FROM $tbl_name WHERE id = ".$condition); return @mysql_result($result, 0); } $myValue = getField($columname,$tablename,$userId); echo "$myValue"; //doDecryption(); } echo '<script type="text/javascript"> doDecryption(); </script>'; echo "whats happening"; ?> </body> </html> [/syntax] <body> CODE THAT IS SENT AS HTML OUTPUT TO THE BROWSWER Code: [Select] <!doctype HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>AES (Rijndael) Encryption Test in JavaScript</title> <script src="aes-enc.js" type="text/javascript" language="JavaScript"></script> <script src="aes-dec.js" type="text/javascript" language="JavaScript"></script> <script src="aes-test.js" type="text/javascript" language="JavaScript"></script> <script type="text/javascript" language="JavaScript"> function doDecryption() { document.alert("Inside Javascript"); var ct, key; var theForm = document.forms[0]; blockSizeInBits=128; keySizeInBits = theForm.keySize[theForm.keySize.selectedIndex].value; if (theForm.key.value.toLowerCase().indexOf("0x") == 0) theForm.key.value = theForm.key.value.substring(2); if (theForm.ciphertext.value.toLowerCase().indexOf("0x") == 0) theForm.ciphertext.value = theForm.ciphertext.value.substring(2); if (theForm.key.value.length*4 != keySizeInBits) { alert("For a " + keySizeInBits + " bit key, the hex string needs to be " + (keySizeInBits / 4) + " hex characters long."); if (theForm.key.select) theForm.key.select(); return; } if (theForm.ciphertext.value.length*4 != blockSizeInBits) { alert("For a " + blockSizeInBits + " bit block, the hex ciphertext string needs to be " + (blockSizeInBits / 4) + " hex characters long."); if (theForm.ciphertext.select) theForm.ciphertext.select(); return; } ct = hex2s(<br /> <b>Notice</b>: Undefined variable: myValue in <b>C:\wamp\www\AES4U\Decryptedresults.php</b> on line <b>41</b><br /> ); document.write("Inside Javascript"); document.write(ct); // key = hex2s(theForm.key.value); // theForm.plaintext.value = byteArrayToHex(rijndaelDecrypt(ct, key, "ECB")); } </script> </head> <body> <br /> <b>Notice</b>: Undefined index: userId in <b>C:\wamp\www\AES4U\Decryptedresults.php</b> on line <b>58</b><br /> // Do not worry about this notice because teh value has not been posted it is not able find this value. </body> can someone help me I am trying to call a class function using a varible.... example say i go to index.php?page=contact i want to pull that class with out doing a switch statement can someone help me Code: [Select] <?php $Site = page; $Site->getPage($_GET['page']); ?> Code: [Select] <?php class page extends site { function getPage($page) { return $this->$page; } function Contact() { echo '<h2>Contact test</h2>'; echo '<form> </form>'; } } ?> Hi all, I have a regular comment section and i want the submit button to call a mysql_query function in comment.class.php so Code: [Select] <form method="post" action="../php/comment.class.php?action=addcomment" name="commentform" id="commentform" onsubmit="validateForm()"> <label for "body">Your Comment</label> <div><textarea cols="20" rows="4" name="body" id="body"></textarea></div> <input type="submit" id="submit" value="Submit"/> and my comment.class.php is... Code: [Select] function addcomment($body){ $body=mysql_real_escape_string($_POST['body']); if($body!=''){ mysql_query("INSERT INTO comment(uid,screenname,body) VALUES ('$_SESSION[uid]','$_SESSION[screenname]','$body')") or die('lame'); } header("location:../index.php"); } but when I click submit, i'm taken to a blank page with the URL: http://test/php/comment.class.php?action=addcomment any tips? and is there a better alternative to header? Thanks |