sábado, 5 de abril de 2025

Class interface

 // Defining the interface

interface BicycleInterface {

    // Method declarations (no implementations)

    public function changeCadence($newValue);

    public function changeGear($newValue);

    public function speedUp($increment);

    public function applyBrakes($decrement);

    public function printStates();

}






// Implementing the BicycleInterface in a class

class Bicycle implements BicycleInterface {

    private $cadence = 0;

    private $speed = 0;

    private $gear = 1;


    // Implementing methods from BicycleInterface

    public function changeCadence($newValue) {

        $this->cadence = $newValue;

    }


    public function changeGear($newValue) {

        $this->gear = $newValue;

    }


    public function speedUp($increment) {

        $this->speed += $increment;

    }


    public function applyBrakes($decrement) {

        $this->speed -= $decrement;

    }


    public function printStates() {

        echo "Cadence: " . $this->cadence . ", Speed: " . $this->speed . ", Gear: " . $this->gear . "\n";

    }


}


// Example usage

$bicycle = new Bicycle();

$bicycle->changeCadence(50);

$bicycle->changeGear(3);

$bicycle->speedUp(20);

$bicycle->applyBrakes(5);

$bicycle->printStates();  // Outputs: Cadence: 50, Speed: 15, Gear: 3

Clsss Inheritance:

 <?php


// Parent class

class Vehicle {

    public $brand;

    public $model;

    

    // Constructor

    public function __construct($brand, $model) {

        $this->brand = $brand;

        $this->model = $model;

    }


    // Method in parent class

    public function start() {

        echo "The vehicle is starting.\n";

    }

}


// Child class inheriting from the Vehicle class

class Car extends Vehicle {

    public $doors;

    

    // Constructor in child class

    public function __construct($brand, $model, $doors) {

        // Call parent class constructor to initialize properties

        parent::__construct($brand, $model);

        $this->doors = $doors;

    }


    // Method in child class

    public function honk() {

        echo "The car is honking.\n";

    }


    // Overriding the parent method

    public function start() {

        echo "The car is starting.\n";

    }

}


// Creating an instance of the Car class

$car = new Car("Toyota", "Corolla", 4);


// Accessing inherited properties

echo "Brand: " . $car->brand . "\n";  // Outputs: Brand: Toyota

echo "Model: " . $car->model . "\n";  // Outputs: Model: Corolla

echo "Doors: " . $car->doors . "\n";  // Outputs: Doors: 4


// Calling methods

$car->start();  // Outputs: The car is starting.

$car->honk();   // Outputs: The car is honking.


?>


domingo, 23 de marzo de 2025

php class example

 class Hombre {

  private $name;

  private $age;


  // Constructor to initialize the name and age

  function __construct($name, $age = 1) {

    // If the name length is less than 3, generate a unique ID as the name

    $this->name = (strlen($name) < 4) ? $name.'_'.uniqid() : $name;

    $this->age = $age;

    echo "$this->name has been created<br>"; 

  }


  // Method to change the name

  function changeName($name) {

    $this->name = $name;

  }


  // Method to get the name

  function getName() {

    return $this->name; 

  }


  // Method to get the age if needed

  function getAge() {

    return $this->age;

  }

}


// Creating an object with the name "a" and age 34

$m = new Hombre('a', 34);  // name will be replaced with a unique ID


// Changing the name to "Mario Rodriguez"

$m->changeName('Mario Rodriguez');


// Outputting the new name

echo $m->getName();  // Outputs: Mario Rodriguez

miércoles, 19 de marzo de 2025

Insert delete function mode

 <?php

function Updatedb($query){



mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);




// Create a connection to the database


$mysqli = new mysqli("localhost", "my_user", "my_password", "world");




// Check for connection errors


if ($mysqli->connect_error) {


    die("Connection failed: " . $mysqli->connect_error);


}




// Check if table exists before creating it


//if ($mysqli->query("SHOW TABLES LIKE 'myCity'")->num_rows == 0) {


    // If the table doesn't exist, create it by duplicating the structure of the 'City' table


  //  if (!$mysqli->query("CREATE TABLE myCity LIKE City")) {


    //    die("Error creating table: " . $mysqli->error);


    //}


//} else {


  //  echo "Table 'myCity' already exists.\n";


//}




// Insert a new record into 'myCity'


//$query = "INSERT INTO myCity (ID, Name, CountryCode, District, Population) VALUES (NULL, 'Stuttgart', 'DEU', 'Stuttgart', 617000)";


if ($mysqli->query($query)) {


    printf("New record has ID %d.\n", $mysqli->insert_id);


} else {


    echo "Error inserting record: " . $mysqli->error;


}




// Close the connection


$mysqli->close();



}

//$query = "INSERT INTO myCity (ID, Name, CountryCode, District, Population) VALUES (NULL, 'Stuttgart', 'DEU', 'Stuttgart', 617000)";

Updatedb($query);

?>


martes, 18 de marzo de 2025

select function

   <?php

function select($query){


mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);  // 1 enable reporting


$mysqli = new mysqli("localhost", "my_user", "my_password", "world");  // 2 connect to the db


if ($mysqli->connect_error) {                                     // 3 check connection error

    /* Use your preferred error logging method here */

    error_log('Connection error: ' . $mysqli->connect_error);

    exit();

}


//$query = "SELECT Name, CountryCode FROM City ORDER BY ID LIMIT 3";  // 4 corrected query


// Execute the query and check for errors

$result = $mysqli->query($query); 

if (!$result) {  // Check if the query failed

    printf("Error message: %s\n", $mysqli->error);

    exit();

}


$mysqli->close();  // 5 Close the connection as soon as it's no longer needed


// Fetch results as an associative array

return $rows = $result->fetch_all(MYSQLI_ASSOC);  // 6 print result as assoc array

//foreach ($rows as $row) {

  //  printf("%s (%s)\n", $row["Name"], $row["CountryCode"]);

//}

print_r(select($query =$argv[1]\));


?>


 php select.php "select * from users;" 

 Array

(

    [0] => Array

        (

            [userId] => 60

            [phoneNumber] => 12127960900

            [accessCode] => 593161

            [pin] => 1234

            [nameURL] => https://rdasteriskrecordings.s3.amazonaws.com/12127960900.wav

            [lastCalled] => 2025-03-06

            [userLang] => en

            [userAuthMode] => cid

        )


    [1] => Array

        (

            [userId] => 61

            [phoneNumber] => 100001

            [accessCode] => 406366

            [pin] => 1234

            [nameURL] => https://rdasteriskrecordings.s3.amazonaws.com/2100001.wav

            [lastCalled] => 2025-03-06

            [userLang] => en

            [userAuthMode] => cid

        )


)


lunes, 10 de marzo de 2025

mysqi step by step

 <?php


mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);  // 1 enable reporting


$mysqli = new mysqli("localhost", "my_user", "my_password", "world");  // 2 connect to the db


if ($mysqli->connect_error) {                                     // 3 check connection error

    /* Use your preferred error logging method here */

    error_log('Connection error: ' . $mysqli->connect_error);

    exit();

}


$query = "SELECT Name, CountryCode FROM City ORDER BY ID LIMIT 3";  // 4 corrected query


// Execute the query and check for errors

$result = $mysqli->query($query); 

if (!$result) {  // Check if the query failed

    printf("Error message: %s\n", $mysqli->error);

    exit();

}


$mysqli->close();  // 5 Close the connection as soon as it's no longer needed


// Fetch results as an associative array

$rows = $result->fetch_all(MYSQLI_ASSOC);  // 6 print result as assoc array

foreach ($rows as $row) {

    printf("%s (%s)\n", $row["Name"], $row["CountryCode"]);

}


?>


sábado, 8 de marzo de 2025