Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Database {
- protected $userName;
- protected $password;
- protected $dbName;
- public function __construct ( $UserName, $Password, $DbName ) {
- $this->userName = $UserName;
- $this->password = $Password;
- $this->dbName = $DbName;
- }
- }
- // and you would use this as:
- $db = new Database ( 'user_name', 'password', 'database_name' );
- class Person {
- public function __construct() {
- // Code called for each new Person we create
- }
- }
- $person = new Person();
- class Person {
- public $name = '';
- public function __construct( $name ) {
- $this->name = $name;
- }
- }
- $person = new Person( "Joe" );
- echo $person->name;
- class Cat
- {
- function Cat()
- {
- echo 'meow';
- }
- }
- class Cat
- {
- function __construct()
- {
- echo 'meow';
- }
- }
- $cat = new Cat();
- class cat {
- function speak() {
- echo "meow";
- }
- }
- include('class_cat.php');
- mycat = new cat;
- $speak = cat->speak();
- echo $speak;
- <?php
- // The code below creates the class
- class Person {
- // Creating some properties (variables tied to an object)
- public $isAlive = true;
- public $firstname;
- public $lastname;
- public $age;
- // Assigning the values
- public function __construct($firstname, $lastname, $age) {
- $this->firstname = $firstname;
- $this->lastname = $lastname;
- $this->age = $age;
- }
- // Creating a method (function tied to an object)
- public function greet() {
- return "Hello, my name is " . $this->firstname . " " . $this->lastname . ". Nice to meet you! :-)";
- }
- }
- // Creating a new person called "boring 12345", who is 12345 years old ;-)
- $me = new Person('boring', '12345', 12345);
- // Printing out, what the greet method returns
- echo $me->greet();
- ?>
- // Create a new class, and include a __construct method
- class Task {
- public $title;
- public $description;
- public function __construct($title, $description){
- $this->title = $title;
- $this->description = $description;
- }
- }
- // Create a new object, passing in a $title and $description
- $task = new Task('Learn OOP','This is a description');
- // Try it and see
- var_dump($task->title, $task->description);
- class Person{
- private $fname;
- private $lname;
- public function __construct($fname,$lname){
- $this->fname = $fname;
- $this->lname = $lname;
- }
- }
- $objPerson1 = new Person('john','smith');
- class Test
- {
- function __construct($value1,$value2)
- {
- echo "Inside Construct";
- echo $this->value1;
- echo $this->value2;
- }
- }
- //
- $testObject = new Test('abc','123');
Add Comment
Please, Sign In to add comment