Jump to content


Check out our Community Blogs

papabear

Member Since 08 Apr 2012
Offline Last Active Apr 03 2017 09:08 PM
*****

#675695 Creating A Simple Yet Secured Login/registration With Php5

Posted by papabear on 20 October 2014 - 05:21 PM

I'm so glad that there are so many people who liked this simple tutorial.Please enjoy yourself and have a great day coding.

 

@sschuctown, just create a profile page and store a session and check if the session is valid using IF statement, then redirect the user into his profile page.


  • 1


#644950 Using Pdo For Database Access (Beginner)

Posted by papabear on 21 November 2012 - 03:11 AM

Thanks!


Have fun :D
  • 1


#644330 PHP OOP Tutorial from beginner to advance - (Encapsulation - Part 9)

Posted by papabear on 13 November 2012 - 05:05 AM

Hello everyone and welcome again into my tutorial, we are now at the part 9 of the PHP Object Oriented Basic Tutorials and we are going to tackle today is about Encapsulation. What Encapsulation is? When to use them and why?


What is Encapsulation?
Encapsulation is just when you want to hide stuffs into your object from the public. It's the idea of hiding information. For example you have an Animal Class, and it has a function called getName() then you've created a subclass of Animal called Cow. Now, when we used the function getName() it will return the name of the animal which is Cow. Now here's the problem What if someone created an Animal Object? Would you allow? Since it's a base class that some classes will inherit it's generic functions.. you wouldn't want someone to create an object from this class right?

Here comes Encapsulation to solve our problem :)

Please if you haven't read the lesson about Inheritance.. please go here and read it Inheritance and __construct() part5



We have three keywords that are involved in Encapsulation, namely

Protected - the method/function/property is available to the parent class and all inheritting class or we call then subclasses or child classes

Private - the method is private and only available to the parent class/base class.
Public - The method is publicly available and can be accessed by all subclasses.

Those three words are not so much difficult to understand correct? It's something like.. for example you are a parent and you want to Protect your children, so it means your children can only be accessed by your family members... >_< makes sense?

If you have something like a secret and you don't want anyone to let it know.. for sure you will keep it Private.

If you have a paper and when exam or quiz time your teacher ask everyone in the class to bring out one whole piece or paper, your classmates will go into you and you will share your paper Publicly (lol).

Now let's take a look on how to use those three keywords.

create 3 files
untitled.JPG

index.php
animal.php
cow.php

here's the content of animal.php



<?php
    class Animal {
     public function getName($arg) {
     echo "This is the ".$arg." Class";
     }
    }
?>


in this animal class we have 1 public function called getName() with an argument or parameter.
it's public so? You're right everything can accessed that function as long as I included that php file.


for the cow.php



<?php
class Cow extends Animal{
public function sayName() {
$this->getName(get_class($this));
}
}
?>

in this I created an Animal class and we used the function get_class() to get the name of the class
notice that I've also it extends to the Animal class.. it means that this class is a subclass or Animal class so everything in the Animal class is shared in this class unless they're not private.

here's the content of index.php

<?php
include_once('animal.php');
include_once('cow.php');

$cow = new Cow();
$cow->sayName();
?>

in this file, I've included animal.php and cow.php so we can use them both in this php file,

first I've created an object

$cow = new Cow();

then I called the sayName(); in the $cow object.. and the sayName() function calls the getName() function, we all know that getName() function is a function from the parent class of Cow which is Animal
so the output would be


This is the Cow Class


so what happens if we created an Animal object?


$animal = new Animal();
$animal->getName("blah");

output :

This is the blah Class



oooppsss that's bad since I only want that getName() function to be accessed from all the subclasses of Animal only.. so I have to change it from Public to Protected


<?php
    class Animal {
     protected function getName($arg) {
     echo "This is the ".$arg." Class";
     }
    }
?>

now what will happened if we created an Animal object like we did before?

output :

Fatal error: Call to protected method Animal::getName()

uh oh.. we got an error because we can't just accessed that because it's protected and only it's base class and subclasses can access that method.

what if we made it private and we call that function inside our cow.php?

animal.php

<?php
    class Animal {
     private function getName($arg) {
     echo "This is the ".$arg." Class";
     }
    }
?>

cow.php


<?php
class Cow extends Animal{
public function sayName() {
$this->getName(get_class($this));
}
}
?>

index.php


<?php
include_once('animal.php');
include_once('cow.php');

$cow = new Cow();
$cow->sayName();
?>


here's the output if you tried to run it

Call to private method Animal::getName()

ofcourse.. you can't call that method because it's private and can only be accessed by it's base class.


Conclusion
Today we've learned the use of encapsulation, this technique in programming is really useful if we are creating a function or some variables that we want to hide from some users.. It's good if you are creating a framework, you wouldn't want some programmers messed up your code and use it the wrong way. Thank you so much for reading my tutorial.
  • 1


#644060 When should I use exceptions?

Posted by papabear on 09 November 2012 - 04:07 PM

Exception is short for "exceptional events" it occurs during the execution/running time of a program and can disrupts the normal flow of the program's instructions. You are correct about exception being used in try and catch to trap errors or handle errors, if there's no try and catch or error handling technique implemented into a method or code that has an error for example an Index out of bounce we all know that this error occurs if the index that we are trying to use or put value into is not existing or out of range like we have 10 index and we are looking for the 11th index.. that's an error. Java will now create an object and hands it off to the runtime system and it's called "Exception Object" that contains all the information about the error, the error message, the error number etc... Most of the time we use the word throw to throw away those exceptions and let our program run even if it has Exceptional Events. Most people use the technique of handling errors with Try and Catch.. this tells to the compiler that

hey try this snippet of code and then catch the error if there are. :)

have fun
  • 1


#643869 PHP OOP Tutorial from beginner to advance - (Static Class - Part 7)

Posted by papabear on 07 November 2012 - 07:26 AM

Welcome again in my tutorial, phew, we are at the part 7 now and we are going to study about those Static Classes, if you are a java programmer or c# or anything else.. you might have heard of the word Static... what are those? Here's a very short explanation and how to use them.

Static
Static Classes are classes that can be accessed without the need of instantiating it. It means that all public methods declared inside that class is available for use without the need of instantiating something like

<?php
   $object = new ClassName();
?>


There's a no need for that.

Note that unlike the instantiated class has own values in it's object while the static class remains its value.
The static instance of a class only happens once and its variables are available into any instance

When to use Static Class?
You can use static class when you just want to create a tool that works for one specific thing, like a converter.
there are also disadvantage of using this.

Hard to Test.
Hard to Extend.

those are in my opinion,.

How to create Static Class in php?

to create static classes, it must have a static function inside it

<?php
      class myClass{
             public static function sayHi() {
                  echo "Hi";
             }
     }
?>


How to use that function? Do you remember how we use the function into an instantiated class? we use the
-> something like an arrow correct? In Static Class we can call a function using

ClassName::Staticfunction

we use a 2 colon between the name of the class and the name of the static function instead of using ->

example :

I have two php file the myClass.php that contains the static function and the index.php is the file where I'm going to call the function of myClass.php

we will use the code above that we've created earlier for the myClass.

now for the index.php

<?php
          require_once('myClass.php');
          myClass::sayHi();
?>


output :

Hi





Conclusion :
Static type functions or variables can only be useful when we are creating something like a global variable or function because it retains the values and only one value, unlike the instance.. we can create many instance of a class and give them different values. I know it's confusing just practice and practice and be careful on using them you might get confused,

suggested article to read about statics
http://kore-nordmann...ed_harmful.html
  • 1


#643725 Email Validation in PHP

Posted by papabear on 06 November 2012 - 03:59 AM

are you asking on how you can validate or check a given string if it's email of not? Most of the time PHP developers use a special Regular Expression for this to check weather a given string is an email but since the people who are under the development of the language introduced PHP 5

there is now a special function that you can use to check if it's email or not
please take a look at this link

PHP Validate Filters
  • 2


#643538 Need help with c++ Express program for a class - beginner

Posted by papabear on 04 November 2012 - 02:53 AM

have you already constructed the loop? Can we see it?
you want to loop 0 - 127 and 16 per line

Hint : you need nested loop

please let us see your progress :)
  • 1


#643460 More then 1 tag for 1 meaning

Posted by papabear on 03 November 2012 - 03:40 AM

Yes you are correct about those tag having the same effect on most normal web browsers but there are important differences between them

<b> and <i> are style tags or for formatting only.. yes we see same effect as <strong> and <em> but in search engine optimization it's different.. The web crawler will treat your <b> and <i> words as bold and italic only, without any meaning it thinks that the developer just style the text..

but to give more importance into a text you use <strong> or <em> to define an emphasized text.

for example you do something like

<b>a</b>
<strong>b</strong>

yes you will see the same result but if you convert that into a sentence

Make the letter a BOLD.
Make b stronger than other text because it's important.


HTML was never really meant about styling... it's the job of CSS.

if you don't care about SEO or Search Engine Optimization, you won't really care about the difference of those tag, but if you want to develop a search engine friendly website, it's good to follow the Web Semantics by W3C
  • 1


#643446 The best type of file for develope databases

Posted by papabear on 02 November 2012 - 03:43 PM

you might be asking for suggestion on what RDBMS(Relational Database Management System)
it is a system that handles databases, you have to learn SQL or Structured Query Language in different websites, once you learned the basic queries.. you are all set, you just need to figure out how to connect your RDBMS often called database..
into your programming language, in connecting you'll meet a friend called ConnectionString good luck :)
  • 1


#641125 PHP OOP Tutorial from beginner to advance - (Inheritance and __construct() -...

Posted by papabear on 08 October 2012 - 07:38 AM

Hello everyone, I'm so sorry about the late updates on my PHP Object Oriented Programming Toturial Series, as I'm experiencing some real life problems.. anyway, let's forget about those problems for now and let's learn new things about PHP Object Oriented and apply it into our work :)


Inheritance and __construct()

this are the two things that we are going to study today in this tutorial, I'll be teaching you what Inheritance means in programming and how to implement it in PHP so we can take the advantage of its power, when to use it and why use it. We are also going to tackle what a Constructor is so we are going to know what is the __construct() means.

before everything we have to familiarize our self to the word Inheritance in Object Oriented Programming,

What is Inheritance?

Inheritance is a way to reuse code of existing objects, or to create an object using the other object.
What do I mean by creating an object from another object?

Yes, It's like parent and child... Real world example : your parent created you, and you inherit some of their appearance or behaviors (Silly but true..)

It's also the same in Inheritance.. there is a parent CLASS and a child CLASS
we are going to call the parent class "Superclass" and the child class "Subclass"

take a look at this and understand

Untitled-1.jpg

In the image above I have a SuperClass Car with 2 SubClass Racecar and Truck I know it's a bad example but please bare with me :)

A car has generic features that every car has.. for example the Color, horsepower and #of seats... we can also use the tire.. because every car has this properties, and there are some properties that is specific to a given car... for example the Racecar.. it has nitro while truck doesn't have.. Truck have trailer while Racecar doesn't have.. right? In that case we are going to write those attributes in each subclass, not in the Superclass..

In traditional way of coding.. think that we are making a Car driving game and we have this classes, without Inheritance we are going to create different classes and give those classes their attributes.. it's like we are retyping the same code all over again for all the same attributes.. so why not create a class that would share properties into each of the Cars?

In PHP way, we make use of the reserve word "extends" to inherit our subclass into a superclass.. for example :

Car.php
<?php
     class Car {
            protected $color;
            protected $horsepower;
            protected $seats;
     }
?>



Racecar.php
<?php
    class Racecar extends Car{

            public function __construct() {

                      $this->color = "Red";
                     $this->horsepower = 200;
                     $this->seats = 2;
            }

}

?>



Truck.php
<?php
    class Truck extends Car{


            public function __construct() {

                      $this->color = "Black";
                     $this->horsepower = 150;
                     $this->seats = 4;

            }


    }
?>



index.php
<?php
      include_once('Car.php');
      include_once('Racecar.php');
      include_once('Truck.php');

      $racecar = new Racecar();
      $truck = new Truck();
?>


So what is this code doing?
First We've created the parent class and think of the attributes/properties that every Car has and added them inside it and made them protected because we don't want to access the properties of this Class directly outside, but we want to access them inside the subclass of the Car..

After that we've created 2 new class named "Racecar.php" and "Truck.php" those are the subclasses of the superclass "Car.php" . You'll notice that I've added the magic word "extends" after the subclass name and the name of the parent Class which is Car so it's something like this

<?php
         class Name_of_Class extends Parent_Class {

        }
?>



Please take a look at the above example and you'll notice that there is something new into your eyes... yes.. there is a magic method

__construct()

this is a constructor.. what is a constructor you ask? It is a function that will be called first whenever you created an instance of the class.. it means that when we instantiate the Racecar and Truck inside the index.php the __construct() method is called and executed.. it is a good place if you want to give default values for your variables/properties or to call methods/functions first...

Inside the __construct() method/function I initialize some default values for the racecar and truck.. you know what the word $this means? Please go into this tutorial ($this operator)

lastly we have created a class called index.php everyone knows that when a website loads.. the index file is the first one that will be called so I've included all the classes into this file so it's all linked :)
this is where I initialize my objects also..


Conclusion
most of the games are using this Inheritance technique to inherit a function or a property into another class.. in this way we can create a class of reusable code for a certain use and then the development of the game for other classes would be easy because there are already made codes that we can just call. :)

Thank you so much...

PS: It's my Birthday today ^_^
  • 1


#640481 javascript vs html5

Posted by papabear on 02 October 2012 - 03:26 AM

I agree with orjan.. they're not suppose to battle each other :)
plus HTML is a markup language not a programming or scripting language..
I've never dreamed of W3C making HTML a programming/scripting language.. that would be a nightmare for us web developer :)
It's better to separate those two... The Scripting language and The MarkUp Language...
so we can create a good web application and distinguish easily what is the scripting file and the html file, in that way.. editing/updating your code would be easier :)
  • 1


#640104 C# and Java Book and Reference Recommendations

Posted by papabear on 28 September 2012 - 07:25 AM

for C# I recommend this book

Essential C#

http://www.amazon.co...1QXDWGVQVHCA361

please have a look into it
  • 1


#640101 PHP OOP Tutorial from beginner to advance - (Scope and Calculator - Part 3)

Posted by papabear on 28 September 2012 - 07:13 AM

Hello everyone and welcome into my PHP OOP Tutorial part 3 and today we are going to make a very very simple
calculator class and to learn about Scopes.

so first let's make a new class and call it Calc.

<?php
class Calc {
public $num;
public $num2;
public $output;

function setNum($input) {
$this->num = (int) $input;
}

function setNum2($input) {
$this->num2 = (int) $input;
}
}
?>


In our class I've added 3 public variables :


public $num;
public $num2;
public $output;


this public variables or global variables or whatever you want to call them.. will be used to store the values of the numbers that we are going to calculate and the answer.

After the variables I've created 2 functions :


function setNum($input) {
$this->num = (int) $input;
}

function setNum2($input) {
$this->num2 = (int) $input;
}


setNum($input) - we are going to use this function to set the value of the public $num variable. $input is a parameter. Why did I put (int) before the $input? It's called Typecasting :) Why? Because we all know that PHP is a loosely typed language and assigns types to variable depending what is assigned to it. What is Typecasting by the way? It is a way of casting the variable type like integer, string, double etc... we are working on whole numbers so we have to make sure that $input is a string so we typecast it :)

let's add some basic math operations functions into our class Calc now..



<?php
class Calc {
public $num;
public $num2;
public $output;

function setNum($input) {
$this->num = (int) $input;
}

function setNum2($input) {
$this->num2 = (int) $input;
}

function addNum() {
$this->output = $this->num + $this->num2;
}


function subtractNum() {
$this->output = $this->num - $this->num2;
}


function multiplyNum() {
$this->output = $this->num * $this->num2;
}


function divideNum() {
$this->output = $this->num / $this->num2;
}



}

?>



we now have a Add, Subtract, Multiply and Divide functions that we can use to calculate our 2 numbers :D



function addNum() {
$this->output = $this->num + $this->num2;
}


function subtractNum() {
$this->output = $this->num - $this->num2;
}


function multiplyNum() {
$this->output = $this->num * $this->num2;
}


function divideNum() {
$this->output = $this->num / $this->num2;
}







Now let's create a function that will echo out our $output :)



<?php
class Calc {
public $num;
public $num2;
public $output;

function setNum($input) {
$this->num = (int) $input;
}

function setNum2($input) {
$this->num2 = (int) $input;
}

function addNum() {
$this->output = $this->num + $this->num2;
}


function subtractNum() {
$this->output = $this->num - $this->num2;
}


function multiplyNum() {
$this->output = $this->num * $this->num2;
}


function divideNum() {
$this->output = $this->num / $this->num2;
}

function getResult() {
echo $this->output;
}

}
?>


here's the function that I've just added


function getResult() {
echo $this->output;
}


so everything is ok now.. let's now use our class :)



<?php
class Calc {
public $num;
public $num2;
public $output;

function setNum($input) {
$this->num = (int) $input;
}

function setNum2($input) {
$this->num2 = (int) $input;
}

function addNum() {
$this->output = $this->num + $this->num2;
}


function subtractNum() {
$this->output = $this->num - $this->num2;
}


function multiplyNum() {
$this->output = $this->num * $this->num2;
}


function divideNum() {
$this->output = $this->num / $this->num2;
}

function getResult() {
echo $this->output;
}

}

$calc = new Calc();
$calc->setNum(1);
$calc->setNum2(2);
$calc->addNum();
$calc->getResult();

?>


output :

3



Some explanation on what happened..

First we've instantiate our calc
$calc = new Calc();

Then we set the value for the $num and $num2
$calc->setNum(1);
]$calc->setNum2(2);

Then we've called the addNum function to add the $num and $num2 and store the result into the variable called $output
$calc->addNum();

Then we want to know the output that's why we called the function :
$calc->getResult();

Let's go now into the SCOPE thing.. everything in our class is now public so that means everything can be changed and access.. for example I can assign a value into the $output variable

$calc->output = 5;

and we don't want the user to assign a value into the out and to the $num and $num2 what are we going to do? That's what SCOPE is.. SCOPE is where you limit the members of a class like functions or variables.. if you want them to be access outside the class or in other class, you declare them as public... if you don't want you make them private or protected.





What is the difference of private and protected? Private Variables of functions can only be accessed inside the class.. while Protected variables and functions can be accessed inside the class and inside the child classes :) were going to tackle about child classes soon..



private $num;
private $num2;
private $output;



We are now done :)
We have a very very simple calculator now...

More Powers
Papabear

Previous Tutorial :
Properties and $this - Part 2

Next Tutorial :
Creating a File Logger - Part4
  • 1


#640043 Get a shoutbox or something of the sort?

Posted by papabear on 27 September 2012 - 05:48 PM

The admin had implemented a chatbox before :)
even if you put a message or rules, there would be someone who will break it.. even if you put an anti-spam, there will be a way to advertise your links for example if you make the URL filtering,, they can just change to dots to (.) or a comma,
if you create a 1 min waiting time before someone can reply, that would be boring for a chatbox...

Private messages or reply into posts.. depends if the one you are messaging is busy or doing something :D
it's the same as the chatbox.. if he/she is busy it will take time before he/she will reply,
  • 1


#640037 Get a shoutbox or something of the sort?

Posted by papabear on 27 September 2012 - 04:50 PM

Here are the reasons why Chatbox is not good for forums

- Chatbox kills Forum activity - most of the user will start asking in chatbox instead of creating a new thread for it..

- Chatbox is good for spammers- Spammers loves chatboxes :D everyone knows that..


Why put a chatbox while we can message a member, add them into our friend list or post a new thread or by replying to posts? That's socializing :D
  • 2




Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download