Search
 
SCRIPT & CODE EXAMPLE
 

PHP

php classes

class Person
{
    public $name = '';
}

$person = new Person();
$person->name = 'bob';

echo $person->name;
Comment

how to make classess in php

<?php
class house {
  public $color;
  public $land;	
  public function __construct($color, $land) {
    $this->color = $color;
    $this->land = $land;
  }
  public function message() {
    return "My house is " . $this->color . " in color and occupies" . $this->model . " of land!";
  }
}

$House = new house("black", "40 meter squares");
echo $House -> message();
echo "<br>";
$House = new house("red", "30 meter square");
echo $House -> message();
?>

//Another example:

class Fruit {
  public $fruitname;
  public $type;
  public function __construct($fruitname, $type) {
    $this->color = $fruitname;
    $this->model = $type;
  }
  public function message() {
    return "The fruit is a " . $this->fruitname . "and in type, " . $this->type;
  }
}

$Fruit = new Car("mango", "Carabao");
echo $Fruit -> message();
echo "<br>";
$Fruit = new Car("apple", "Red Delicious");
echo $Fruit -> message();
?>
Comment

php class


<?php
/**
 * Define MyClass
 */
class MyClass
{
    public $public = 'Public';
    protected $protected = 'Protected';
    private $private = 'Private';

    function printHello()
    {
        echo $this->public;
        echo $this->protected;
        echo $this->private;
    }
}

$obj = new MyClass();
echo $obj->public; // Works
echo $obj->protected; // Fatal Error
echo $obj->private; // Fatal Error
$obj->printHello(); // Shows Public, Protected and Private


/**
 * Define MyClass2
 */
class MyClass2 extends MyClass
{
    // We can redeclare the public and protected properties, but not private
    public $public = 'Public2';
    protected $protected = 'Protected2';

    function printHello()
    {
        echo $this->public;
        echo $this->protected;
        echo $this->private;
    }
}

$obj2 = new MyClass2();
echo $obj2->public; // Works
echo $obj2->protected; // Fatal Error
echo $obj2->private; // Undefined
$obj2->printHello(); // Shows Public2, Protected2, Undefined

?>

Comment

class php


<?php
class Foo {
    public $aMemberVar = 'aMemberVar Member Variable';
    public $aFuncName = 'aMemberFunc';
   
   
    function aMemberFunc() {
        print 'Inside `aMemberFunc()`';
    }
}

$foo = new Foo;

function getVarName()
{
     return 'aFuncName'; 
}

print $foo->{$foo->{getVarName()}}();

Comment

php define class

class Bike {
    	function Bike() {
            $this->type = 'BMX';
    }
}

$blackSheep = new Bike();

print $blackSheep->type;
Comment

php class


/*PHP 5 is very very flexible in accessing member variables and member functions. 
These access methods maybe look unusual and unnecessary at first glance; 
but they are very useful sometimes; 
specially when you work with SimpleXML classes and objects. 
I have posted a similar comment in SimpleXML function reference section, 
but this one is more comprehensive.

I use the following class as reference for all examples:
*/

<?php

class Foo {

    public $aMemberVar = 'aMemberVar Member Variable';
    public $aFuncName = 'aMemberFunc';


    function aMemberFunc() {
        print 'Inside `aMemberFunc()`';
    }
}
$foo = new Foo;
?>
/*You can access member variables in an object using another variable as name:*/

<?php
$element = 'aMemberVar';
print $foo->$element; // prints "aMemberVar Member Variable"
?>
or use functions:

<?php

function getVarName()
{ return 'aMemberVar'; }
print $foo->{getVarName()}; // prints "aMemberVar Member Variable"

?>



//Important Note: You must surround function name with { and } 
  ///or PHP would think you are calling a member function of object "foo".
//you can use a constant or literal as well:

<?php

define(MY_CONSTANT, 'aMemberVar');

print $foo->{MY_CONSTANT}; // Prints "aMemberVar Member Variable"

print $foo->{'aMemberVar'}; // Prints "aMemberVar Member Variable"

?>
//You can use members of other objects as well:

<?php

print $foo->{$otherObj->var};

print $foo->{$otherObj->func()};

?>

//You can use mathods above to access member functions as well:

<?php

print $foo->{'aMemberFunc'}(); // Prints "Inside `aMemberFunc()`"

print $foo->{$foo->aFuncName}(); // Prints "Inside `aMemberFunc()`"

?>

Comment

php ::class

use AppConsoleCommandsInspire;

protected $commands = [
    Inspire::class, // Equivalent to "AppConsoleCommandsInspire"
];
Comment

PREVIOUS NEXT
Code Example
Php :: slim disable display error details 
Php :: wordpress html classes 
Php :: old codestar text field 
Php :: php datenbank beschreiben 
Php :: codeigniter admin panel with crud generator - 
Php :: wordpress set category front end 
Php :: laravel postgres deadlock 
Php :: Array unpacking support for string-keyed arrays - PHP 8.1 
Php :: fpdf tutorial php mysql 
Php :: php replace all text from string with associate array values 
Php :: wp php get product attribute name without pa 
Php :: 200usd to php 
Php :: inject multiple logger symfony 
Php :: rename image file using post id in wordpress programmatically 
Php :: .htaccess Preventing access to your PHP includes files 
Php :: import facade laravel 
Php :: an einem string etwas anfügen php 
Php :: php mysql insert record if not exists in table 
Php :: Convert Shamsi Jalali Persian Date TimeStamp 
Php :: Anzeige von Custom Post Types in den Kategorien und Tags-1 
Php :: wp plugin handles - load on specific page 
Php :: if is gutenberg page php wp 
Php :: Installation request for pokemon-tcg/pokemon-tcg-sdk-php ^1.2 - satisfiable by pokemon-tcg/pokemon-tcg-sdk-php[1.2.0] 
Php :: Including ACF in a custom theme or plugin 
Php :: How to remove repetitive values from foreach loop in php laravel 
Php :: php numeros enteros 
Php :: Verifying session info 
Php :: old codestar textarea field 
Php :: désinfecte email php 
Php :: file upload yii2 rest api 
ADD CONTENT
Topic
Content
Source link
Name
5+1 =