CS174
Chris Pollett
Mar 7, 2016
<?php $mysqli = new mysqli("localhost", "my_user", "my_password", "my_db"); // note: here we are using the OO interface to mysqli // we'll talk more about OO in PHP later today; for now, pretend its like Java/C++ $stmt = $mysqli->stmt_init(); if ($stmt->prepare("SELECT FNAME FROM EMPLOYEE WHERE LNAME=?")) { $lnames = ["Smith", "Jones", "Pollett"]; foreach($lnames as $lname) { $stmt->bind_param("s", $lname); //s == string, i == int, d==double $stmt->execute(); $stmt->bind_result($fname); $stmt->fetch(); print("The first person I found with last name $lname was $fname\n"); } $stmt->close(); } $mysqli->close();
SSN, PNUMBER--> HOURS SSN --> ENAME PNUMBER --> PNAME, PLOCATION.
Which of the following statements is true?
class MyClass { public $field_var1; public $field_var2 = 5; ... const $constant1; const $constant2; ... function __construct($arg1, $arg2) { //constructor code } function myFun1() { // code } ... }
require("MyClass.php"); //or more likely require_once("MyClass.php");
public $my_variable = 0;
function __construct($n=0) { $this->my_variable = $n; }
private $my_field; protected function myMethod() { /* some code*/}
var $my_field;This is less clear than the public syntax so should be avoided.
class Foo{ static $bob=1;} echo "bob: ".Foo::$bob;
class Goo{ const blob=1;}
Goo::blob
define("PI", 22/7); echo PI;
$my_copy = clone $my_obj;
class A{} class B extends A {}
interface MyInterface { function method1($a, $b); }
class C implements MyInterfaceA, MyInterfaceB {}
class A {function foo(){} } class B extends A {function foo(){} }
class A {final function foo() {} /* can't override*/ }
trait MyMethods { function getFoo() { /* code for getFoo */ } function getGoo() { /* code for getFoo */ } } class SomeClassA extends NeedsMethods { use MyMethods; //as if I had written the code in MyMethods here /* rest of SomeClassA code */ } class SomeClassB extends NeedsMethods { use MyMethods; /* rest of SomeClassB code */ } $a = new SomeClassA(); $a->getFoo();
class A { use MyMethodsA, MyMethodsB; // ... }
use MyMethodsA, MyMethodsB { MyMethodsA::foo insteadof MyMethodsB; MyMethodsB::goo insteadof MyMethodsA; MyMethodsA::goo as gooey; MyMethodsB::foo as protected fooey; //notice can change visibility if want }
namespace TheNameOfMyNamespace; // code for namespace class A { //code for class A } function foo() { }This declaration with the same name can be at the top of multiple files if you would like to group the contents of these files into the same namespace.
namespace TheNameOfMyNamespace\MySubNameSpace; //code
namespace D { // code for D } namespace E { // code for E }
namespace N; function foo() { //some code } foo(); //assume N namespace.
M\foo(); /*in context of above we are in N namespace already, so M\foo() means the foo() in N\M namespace. */ \N\M\foo(); \time(); //time function in the global (default) namespace.
use A\B\C; //rather than typing A\B\C\foo(); could type foo(); use D\E\F as G; //rather than typing D\E\F\goo(); could type G\goo();
try {} catch(MyException $e){} catch(Exception $ee){}
if($denom == 0){ throw new Exception("divide by zero"); }
function my_gen($start, $end) { for ($i = $start; $i < $end; $i++) { // $i is preserved between yields. yield $i; } } $generator = my_gen(0, 10000); foreach ($generator as $value) { echo "$value\n"; }
yield $k => $v;