Wednesday, September 18, 2013

Zend Eclipse

ALT + CRTL + DOWN ARROW (duplicate rows)
CTRL + / (toggle comment)
ALT + UP / DOWN - ARROWS (Move your selection around)

Saturday, August 10, 2013

Error Handilng

This is what you would want to do on a development server to display errors

<?php
   error_reporting(E_ALL);
?>

To temporarily shut this off

<?php
   error_reporting(0);
?>
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

<?php
   error_reporting(E_ALL);
   $num1 = 1;
   $num2 = 0;
   
   function error_manage($num1, $num2)
   {
      if($num2 == 0)
         {
            throw new Exception("Can't divide by zero");
         }
         return true;
   }
   
   try
   {
      error_manage($num1, $num2);
      echo "$num1 divided by $num2 is "$num1/$num2;
   }

   catch(Exception $e)
   {
      echo "Error: " . $e->getMessage() . "<br>" . $e->getFile() . "<br>" . $e->getLine() . "<br>";  
   }

?>

I probably don't need to do this because most servers have error logs that I can look at.

File I/O

$file = fopen("customers.txt", "r");
   

  • Privileges at the beginning of the file

   r - is for read privilege
   r+ - for read write
   w - just write 
   w+ - will delete content and give you read and write
   a - will give you write privileges


  • Privileges at the end of the file
   a - read
   a+ - read and write as you may have guessed

And that's it! At least until I know why on earth I would want to do this, but supposedly this is very useful.


How to grab stuff off of the web

Okay: I actually checked this and it throws an error but it does create the text file like it's supposed to:)

<?php
$file2 = fopen("http://stormbloom.com", "rt");//rt - read in text
while (!feof($file2))//This condition equals "NOT" to the end of the file so it will continue to execute until the end is reached
{
$stormbloom .= fgetss($file2, 1024);//fgetss strips out tags, 1024 - you want the first 1024 chatracters (just always type this)
}
fclose($file2);//close the file
$file3 = fopen("stormbloom.txt", "wt");//Write the info to text file; give the ability to write text
fwrite($file3, $stormbloom);
fclose($file3);
?>

This is pretty damn cool

_________________________________

____________________________________________

OOP

You can not perform perfect OOP in PHP. It is rather a knock off version.

In OOP, variables are referred to as attributes, and fuctions are methods


Polymorphism doesn't really work in PHP although there are some hackarounds



class ClassName//It is good practice to capitalize the name
{
   //All of the object that you make will automatically be assigned these variables
   public $var1;
   private $var2;
   protected $var3;
}
_________________________________________________________

Encapsulation allows you to hide the code behind an interface of sorts

public: anyone can get the information and make changes to this variable
private: can only be chages by methods or functions that reside within the class
protected: is also encapsulated


class Animal
{
   public $name;//this doesn't need any function because it is public
   private $age;//this will need a function
   const POUNDID = 12345;
}

   //The Constructor File
   public function __construct($age)//user can set the age
      {
         echo "__constructor created";
         $this->setAge($age);
      }
   //This file will be called every time you create a new object
   
   
public function setAge($sentAge)
   {
      $this->age = $sentAge;
   }

   public function getAge()
   {
      return $this->age;
   }

$this is referring to the object itself; age is a reference to the variable age inside of the object. Anytime you want to refer to variables inside of objects you have to use the $this keyword.

________________________________________________________


Create the object


$dog = new Animal();//Creates a new object from of the animal class

$dog->name = "Grover";//Assign a value to the public attribute "$name"

$dogName = $dog->name;//Retrieves the value from the public attribute 

Set the $age


$dog->setAge(8);//Calls the function
echo "The dogs name is" . $dogname . "<br>";
echo "The dogs age is" . $dog->getAge() . "<br>";


class Animal
{   
   private $name;   
   private $favFood = "Meat";  
   
   public static $numOfAnimals = 0;
   public function __construct($name="No Name")
   {
      echo "__constuct was called";
      $this->setName($name);//call the setName function
      self::$numOfAnimal++;//Access the static variable and increase it's value by one
   }


A static attributes value is shared by all objects of a class. If one object changes the value of this shared variable, it is reflected in all the objects the class




Destroy an object when it is no longer needed


   }
   public function __destruct()
   {
     echo "Destroyed";
   }

   Final
   ______________________

   final public function getName()
   {
      return $this->name;
   }

   final public        function setName($sentName)
   {
      $this->name = $sentName;
   }



   Final says not to let this function to be overridden by any    other methods whenever a new class* extends this animal    class


   Inheritance occurs whenever you base a new class from an    existing class. When you extend a class you are given it's    attributes and methods which you may then modify by    overriding certain attributes and classes, however; you cannot    override a function declared as final

   public function makeNoise()
   {
      echo "Grrrr <br>";
   }

   public function favFood()
   {
      echo "My favorite food is " . $this->faveFood . "<br>";

   public function move()
   {
      echo "Walk around <br>";
   }
}//End Animal class

Extend the Animaclass

class Dog extends Animal
{
   public function __constuct($name="Null")
   {
      //Call the parent construct Method
      parent::__construct($name);
      //Or Animal in place of parent
   }
   public function __destruct()
   {
      echo "__Destroy!";
   }    
   //Override a function by recreating it 
   public function makeNoise()
   {
      echo "Ruff! Ruff! <br>";
   }
}

Create a new object of the dog class

$grover = new Dog("Grover");//This is putting the string Grover into the $name attribute which is stored in the public function __constuct parameter, which would otherwise be null
$grover = new Dog("Paul");
$grover->makeNoise();

$grover->favFood();
$grover->move();

echo Animal::$numOfAnimals . "<br>";












Friday, August 9, 2013

Reg-ex

Delimiters /@#`~%&'" try not to use quotes

$searchFor = preg_greg(%the%, $myArray);

foreach($searchFor as $searchResultArray)
{
  echo $searchResultArray, "<br>";
}

\d 0-9
\D excludes 0-9
\w a-z upper and lowercase, numbers, and underscores
\W excludes all \w
\s whitespace
\S excludes all \s
\b spaces between words (word boundaries)
\B excludes all \B


$searchFor = preg_greg(%\d{5}%, $myArray).......
//Search is for 5 digit numbers

$searchFor = preg_greg(%\d{1,5}%, $myArray).........
//Search is for 1-5 digit numbers

$searchFor = preg_greg(%\d{3,}%, $myArray).........
//Search is for 3 or greater digit numbers

$searchFor = preg_greg(%^<.*>(.*)<.*>$%, $myArray)........
//Search is for anything that would appears between two <tags>
//".*" denotes that nay numbers can occur any number of times
//The dollar sign says that at that point (the closing <tag>) the string will end.

$searchFor = preg_greg(%ass[essisture]%, $myArray).......
//Let's say I was looking for: assess, assist, assure

$searchFor = preg_greg(%^do%[^g|ug], $myArray)........
//Search is for don; omits dog or dug

More on this Later.....(Maybe)




Replace text 

<?php
$tweety = "I taught I taw a pooty tat";
$tweety = preg_replace("%taught%", "thought", $tweety);
$tweety = preg_replace("%taw%", "saw", $tweety);
$tweety = preg_replace("%pooty%", "kitty", $tweety);
$tweety = preg_replace("%tat%", "cat", $tweety);
echo $tweety;
?>//There's prolly a better way to do this

<?php
$tweety = "I taught I taw a pooty tat";
$tweety = string_replace("taught", "thought", $tweety);
$tweety = string_replace("taw", "saw", $tweety);
$tweety = string_replace("pooty", "kitty", $tweety);
$tweety = string_replace("tat", "cat", $tweety);
echo $tweety;
?>//There's prolly a better way to do this

        echo substr($tweety, 18, 5);
//This print form the 18th character for 5 characters (kitty)

Functions


___________________________________________________________
echo rand(1,100) . "<br><br>";

You definitely never want to have a dollar sign in front of a function

____________
_______


<?php
function editGlobal($globalVariable)//Pass it in
{
$globalVariable = 200;//Try to change the value
}
$globalVariable = 120;//
echo $globalVariable, "<br><br>";
editGlobal($globalVariable);//Fails
echo $globalVariable, "<br><br>";//Prints the same thing

?>



You need to reference a variable if it is out of scope

<?php
function editGlobal(&$globalVariable)//Reference it
{
$globalVariable = 200;
}
$globalVariable = 120;
echo $globalVariable, "<br><br>";
editGlobal($globalVariable);//Success
echo $globalVariable, "<br><br>";

?>


RECURSION
Calling a function from inside of itself

1.<?php
2. function factorial($number)
3. {//What are passed variables between friends, huh(?)
4. if($number == 1)
5. {
6. return 1;
7. }
8. else
9. {
10. return $number * factorial($number - 1);
11. }
12. }
13. echo factorial(3);

?>

This is similar to a looping. Line 10 calls the function from within itself , subtracting 1 each time then multiplying it by the value of $number until the condition on line 4 is met.